Professional Documents
Culture Documents
ICP - Topic 4 - Operators, Expressions and Satements
ICP - Topic 4 - Operators, Expressions and Satements
Output: 5 6
RULES OF OPERATOR PRECEDENCE
INCREMENT AND DECREMENT OPERATORS
INCREMENT AND DECREMENT OPERATORS
++ is the increment operator
a++
++a
Both of the above expressions are similar to a = a + 1. What is
the difference?
-- is the decrement operator
a--
--a
Both of the above expressions are similar to a = a - 1. What is the
difference?
INCREMENT AND DECREMENT OPERATOR
Pre increment/ Pre decrement: The increment or decrement
happens before ‘a’ is used in an expression.
--a
++a
Post increment/ Post decrement: The increment or decrement
happens after ‘a’ is used in an expression.
a++
a--
QUESTION: WHAT ARE THE VALUES OF A AND B AT THE
END OF EACH OF THE FOLLOWING QUESTIONS?
a is 11 and b is 11 a is 9 and b is 9
a is 11 and b is 12 a is 9 and b is 10
Output: 2 1
00001100 &
00011001
00001000 = 8 (In decimal)
BITWISE OPERATORS
x and y are bits
c = a | 2;
c = b & 8;
c = a ^ b;
SPECIAL OPERATORS: SIZEOF() OPERATOR
The sizeof() operator returns the size of a variable (the number of
bytes). For an example, sizeof(a), where a is an integer, will return
4.
OPERATOR
PRECEDENCE AND
ASSOCIATIVITY
TYPE CONVERSIONS: IMPLICIT CONVERSION
When an operator has operands of different types, they are
converted to a common type according to a small number of rules.
Usually the operand having the lower data type will be converted
into a higher data type. Some of the rules are:
•If either operand is double, all the operands are converted to double.
•If either operand is float, all the operands are converted to is float.
IMPLICIT CONVERSION: EXAMPLE
Output: 5
Output: 35.5
int * float
One of the operands is
float, therefore both of
the operands of the
expression are implicitly
converted to float
before evaluating.
float * float
TYPE CONVERSION: EXPLICIT CONVERSION
It is possible for you to demand the precise type conversion that
you want in C. The method of doing this is called ‘type casting’. It
consists of preceding the quantity with the name of the desired
type in parenthesis.
Examples
EXPLICIT CONVERSION: EXAMPLE
Output: 2.583333…
Output: 3 2
QUESTIONS: WHAT ARE THE OUTPUTS OF THE
FOLLOWING EXPRESSIONS?
int a = 5;
int b = 10;
1. !(a > b) && (b <= 100)
2. ++a * b % 70
3. 7 | 10
4. 56 * 10 % 2
5. a && b
QUESTIONS: WHAT ARE THE OUTPUTS OF THE
FOLLOWING EXPRESSIONS?
int a = 5;
int b = 10;
1. !(5 > 7) && (10 < 8) + 100
2. ++a + b * 7 & 10
3. 7 % b++ * 7 & (7 <= 2)
4. 56 / a == 5
5. a+=5 + 101
QUESTIONS
1. Write a C program to read a character from the keyboard and
print its ASCII value.
2. Write a C program to read an ASCII value from the keyboard
and print its corresponding character.
Q&A