Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

C Fundamentals

Dr. Odelu Vanga

Department of Computer Science and Information Systems


Birla Institute of Technology and Science Pilani
Hyderabad Campus
odelu.vanga@hyderabad.bits-pilani.ac.in

January 31, 2020

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 1 / 10


Today’s Topics

Assignment, Quotient, and Remainder


Character digit to Number digit conversion
Some examples on operators

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 2 / 10


Assignment Operator

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 3 / 10


Assignment Operator

Assignment associates right to left.

y = x = 3;

y gets the value 3, because (x = 3) evaluates to the value 3.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 3 / 10


Assignment Operator

Assignment associates right to left.

y = x = 3;

y gets the value 3, because (x = 3) evaluates to the value 3.


int x, y=5, z=5;
x = y == z;
printf(“%d”, x); precedence of ‘ =0 is less than 0 == ‘.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 3 / 10


Assignment Operator

Assignment associates right to left.

y = x = 3;

y gets the value 3, because (x = 3) evaluates to the value 3.


int x, y=5, z=5;
x = y == z;
printf(“%d”, x); precedence of ‘ =0 is less than 0 == ‘.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 3 / 10


Quotient and Remainder Operator

Quotient: a/b returns quotient, for any a and b


integers.
Example: 2/3 = 0, 3/2 = 1, 5/2 = 2

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 4 / 10


Quotient and Remainder Operator

Quotient: a/b returns quotient, for any a and b


integers.
Example: 2/3 = 0, 3/2 = 1, 5/2 = 2

Modulus (remainder): a%b returns remainder, for


any a and b integers.
Example: 2%3 = 2, 3%2 = 1, 5%2 = 1

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 4 / 10


ASCII Table for Alphabets and Digits

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 5 / 10


ASCII Table for Alphabets and Digits

A character represent within single quotes, ex. ‘A’

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 5 / 10


ASCII Table for Alphabets and Digits

A character represent within single quotes, ex. ‘A’


Each character we call character constant

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 5 / 10


ASCII Table for Alphabets and Digits

A character represent within single quotes, ex. ‘A’


Each character we call character constant
The integer equivalent to the character is its ASCII value

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 5 / 10


ASCII Table for Alphabets and Digits

A character represent within single quotes, ex. ‘A’


Each character we call character constant
The integer equivalent to the character is its ASCII value

printf(“ASCII of A is %d\n”, ‘A’);


Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 5 / 10
Example: Char digit to Number digit

Write an algorithm to convert


a given digit character to digit number

2 + 2 = 4, but ‘2‘ + 2 = 52 ?

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 6 / 10


Example: Char digit to Number digit

Write an algorithm to convert


a given digit character to digit number

2 + 2 = 4, but ‘2‘ + 2 = 52 ?

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 6 / 10


Example: Char digit to Number digit

Read a number as individual character digits,


and convert it into integer
Ex: Read characters ‘1’, ‘2’, ‘3’, ‘4’ and
convert it to integer 1234.

Draw a flowchart for the above algorithm

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 7 / 10


Example

1. Write an algorithm to delete the last digit. input


13613, output 1361. input 324, output 32.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 8 / 10


Example

1. Write an algorithm to delete the last digit. input


13613, output 1361. input 324, output 32.
2. Write an algorithm to delete last two digits. input
13613, output 136. input 324, output 3.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 8 / 10


Example

1. Write an algorithm to delete the last digit. input


13613, output 1361. input 324, output 32.
2. Write an algorithm to delete last two digits. input
13613, output 136. input 324, output 3.
3. Write an algorithm to find the sum of last two
digits. For above inputs, output 1 + 3 = 4 and
2 + 4 = 6.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 8 / 10


Example

1. Write an algorithm to delete the last digit. input


13613, output 1361. input 324, output 32.
2. Write an algorithm to delete last two digits. input
13613, output 136. input 324, output 3.
3. Write an algorithm to find the sum of last two
digits. For above inputs, output 1 + 3 = 4 and
2 + 4 = 6.
4. Write an algorithm to print the second last digit.
input 23617, output 1.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 8 / 10


Example

5. Write an algorithm to double the last digit. e.g.


input 23613, output 23616. input 324, output 328.
(assume that last digit is less than 5)

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 9 / 10


Example

5. Write an algorithm to double the last digit. e.g.


input 23613, output 23616. input 324, output 328.
(assume that last digit is less than 5)
6. Write an algorithm to delete the second last digit.
e.g. input 23617 output 2367.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 9 / 10


Example

5. Write an algorithm to double the last digit. e.g.


input 23613, output 23616. input 324, output 328.
(assume that last digit is less than 5)
6. Write an algorithm to delete the second last digit.
e.g. input 23617 output 2367.
7. Write an algorithm to exchange last two digits.
e.g. input 23617 output 23671.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 9 / 10


Example

5. Write an algorithm to double the last digit. e.g.


input 23613, output 23616. input 324, output 328.
(assume that last digit is less than 5)
6. Write an algorithm to delete the second last digit.
e.g. input 23617 output 2367.
7. Write an algorithm to exchange last two digits.
e.g. input 23617 output 23671.
8. Write an algorithm to exchange last and third last
digit. e.g. input 23617 output 23716.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 9 / 10


.

Thank You

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 31, 2020 10 / 10

You might also like