C Operators, Expressions and Statements                                     Chapter 5


Objectives of this chapter:

Operators

Operator  Description  Associativity 
()  Function Call  Left to Right 
[]  Array element reference  Left to Right 
->  Pointer to structure  Left to Right 
Structure member reference  Left to Right 
Unary Minus  Right to Left 
++  Increment  Right to Left 
--  Decrement  Right to Left 
Logical negation  Right to Left 
One's Complement  Right to Left 
Pointer Reference  Right to Left 
Address  Right to Left 
sizeof  Size of an object in bytes  Right to Left 
Multiplication  Left to Right 
Division  Left to Right 
Modulus  Left to Right 
Addition  Left to Right 
Subtraction  Left to Right 
<<  Left shift  Left to Right 
>>  Right Shift  Left to Right 
Less than  Left to Right 
<=  Less than or equal  Left to Right 
Greater than  Left to Right 
>=  Greater than or equal  Left to Right 
==  Equality  Left to Right 
!=  Inequality  Left to Right 
Bitwise AND  Left to Right 
Bitwise XOR  Left to Right 
Bitwise OR  Left to Right 
&&  Logical AND  Left to Right 
||  Logical OR  Left to Right 
?:  Conditional  Right to Left 
= *= /= %= 

+= -= &= ^= |= <<= >>= 
Assignment Operators  Right to Left 
Comma operator  Left to Right 

Arithmetic Operators :

- (unary) + - * / %  -x Negates the value of x 

x+y Adds x with y 

x-y Subtracts y from x 

x*y Multiplies x by y 

x/y Divides x by y 

x%y Remainder of x divided by y 

Assignment Operators:

= += -= *= /= %= &= ^= != <<= >>=  x=y store the value of y into 

x+=y same as x=x+y 

x-=y same as x=x-y 

Logical Operators:

&& (AND) || (OR) ! (NOT)  x && y has the value of 1 if both x and y are nonzero, and a zero otherwise. y is evaluated only if a is nonzero. 

x || y has the value of 1 if either x or y is nonzero, and a0 otherwise. y is evalueated only if x is zero. 

!x has the value of 1 if x is zero, and zero otherwise. 

Relational Operators:

< <= > >= == !=

x<y has the value of 1 (true) if x is less than y, and o (false) otherwise. 

x<=y has the value of 1 (true) if x is less than or equal y, and o (false) otherwise. 

x>y has the value of 1 (true) if x is greater than y, and o (false) otherwise. 

x>=y has the value of 1 (true) if x is greater or equal than y, and o (false) otherwise. 

x==y has the value of 1 (true) if x is equal y, and o (false) otherwise. 

x!=y has the value of 1 (true) if x is not equal y, and o (false) otherwise. 

Bitwise operators:

& | ^ ~ << >> 

Bitwise compelement : ~

Bitwise AND : &

Bitwise OR: |

Bitwise Exclusive OR: ^

For example: 
#include <stdio.h>

main()
{
        int a,b,c;
        a = 0xABCD;
        b = 0xFF00;

        c = ~a;
        printf("~a = %#X\n",c);
        c = a & b;
        printf("~a = %#X\n",c);
        c = a | b;
        printf("~a = %#X\n",c);
        c = a ^ b;
        printf("~a = %#X\n",c);
}

Output:

~a = 0x5432
a & b = 0xABCD
a | b = OxFFCD
a ^ b = Ox54CD

Increment and Decrement operators

++ --  prefix mode: 

pre-increment or pre-decrement 

the ++ or -- is placed before the affected variable.  example: 

x = 2 * ++y; 
postfix mode: 

post-increment or post-decrement 

the ++ or -- is placed after prefix mode: 

pre-increment or pre-decrement 

the ++ or -- is placed before the affected variable.  example: 

x = 2 * ++y;  example: 

x = 2 * y--;  y++; 

or 

++y; 

Conditional expression operator:

? :  this operator is a trinary operatorm that is it expects three operands. 

a ? b : c 

if the expression a is nonzero (true), then the entire conditional expresion has the same value as the expression b, if the expression a is zero (false), then the entire conditional expression has the same value as the expression c. 

example: 

a=3; 

b=1 

maxtwo = (a>b) ? a : b; 

Type cast operator

(type)  example: 

int x; 

float y; 

x = (int) 5.4; 

y=5.4; 

x=x+(int) y; 

sizeof operator

the sizeof operator returns the number of bytes its operand occupies. 

example 

#include <stdio.h> 

main() 

{ 

int i; 

printf("i has %d bytes\n", sizeof(i)); 

printf(int type varaibles have %d bytes\n", sizeof(int)); 

i = sizeof(int); 

} 

Operator precedence and associativity

example : 

a = b - c * d;  example: 

a = b + c - d 

since the + and - have the same level of precedence but have a left to right associativity, then b+c is done first and then d is subtracted from the result of a+b. 

Type conversions.  The rules are : 
  1. if char and short int (signed or unsigned) are used in an expression, it is automatically converted to int. 
  2. a promotion of a data type occurs when a type is converted to a larger type. 
  3. in any operation involvinig two different types, both values are converted to the "higher" ranking of the two types. 
  4. The ranking of types are as follows (from highest to lowest): 
1