5 Operators Expressions
5 Operators Expressions
02/09/11
Arithmetic Operators
The binary arithmetic operators are : + - * / % The expression x % y produces the remainder when x is divided by y The operands of % should be unsigned integers. Some unary operators are: + There are many other operators, discussion is deferred.
2
02/09/11
The relational operators are > >= < <= == Logical operators are && ||
!= !
02/09/11
Expressions
Expressions combine operands (variables, constants) and operators to produce new values.
Eg: 3 + count*(i+j)
A variable can be initialized using a constant expression. Eg: int total = 2+3*4; Is the value of 3 * 4 + 5 = 17 , Or
= 27
02/09/11
3*4+5
is
((3 * 4) + 5),
but not
(3 * (4 + 5))
3 / 4 / 5 = ((3 / 4) / 5),
but not
(3 / ( 4 / 5 ))
It is because associativity of / is from left to right. - - 4 = (- ( -4) ). This is because associativity of - (unary minus operator) is from right to left.
5
02/09/11
02/09/11
For a complete list of rules, refer Table 2-1, in Page 53 of Kernighan and Ritchie book. What is the value of
4 + 3 * 2 == 9
TRUE is represented with 1 and FALSE with 0 The unary negation operator ! Converts a non-zero operand into 0, and zero operand into 1
So, what is the value of !(2+3 == 4) int i; i = !5; /*what is the value of i */ int i = 15; printf(%d, i = 10); /* what will get on the screen */
7
02/09/11
Expressions
Is the above syntactically valid? What is the value of count ? The operands used in an expression should be ideally of same type. The result of the expression will be of same type as operands type. int i; i = 3/4; /* what will be value of i */ Automatic type conversion is done some times when the operands are of different types.
02/09/11
In 3 + 4.0 3 is converted to float 3.0 But int sum[5]; sum[3.0] = 100; /* wrong, 3.0 is not allowed, because it doesnot make sense */
Expressions that might lose information, like assigning a longer integer type to a shorter, may draw a warning, but they are not illegal.
9
02/09/11
Expressions
What will be the values of i, j and k float i, j; int k; i = 3/2; j = 3.0/2; k = 3.0/2; Conversions take place across assignments; the value of the right side is converted to the type of the left.
10
02/09/11
/* 3 3.0 because of explicit type conversion 3.0/2 3.0/2.0 because of automatic conversion So, f gets value 1.5 */
02/09/11
i = i+2 ; can be written in a compressed form as i += 2; Most binary operators of the form variable = variable op expression can be written like variable op= expression x *= y+1; means x = x * (y+1); and not x = x * y + 1;
02/09/11
12
Conditional Expression
expr1 ? expr2 : expr3 is an expression and has value expr2 if expr1 is non-zero(true), otherwise has value expr3 z = (a > b) ? a : b; /* z = max(a, b) */ x = 5 ? 0:1; /* what is value of x */
02/09/11
13
y = x++ ;
Post increment : Use and then increment Pre increment : increment and then use
y = ++x ;
02/09/11
/*This is illegal */
Check
int a[5], j = /* 0;j = j+1; a[++j] = 4;
a[j] = 4; */
/*So, a[1] = 4 */
02/09/11
15