C Operators, Expressions and Statements
Chapter 5
Objectives of this chapter:
-
C Operators
-
Arithmetic Operators
-
Assignment Operators
-
Logical Operators
-
Relational Operators
-
Bitwise Operators
-
Increement / Decrement Operators
-
Conditional Operators
-
Sizeof Operator
-
Operator Precedence and Associativity
-
Type Conversion and Mixed Expression.
Operators
-
C is rich with numerous operators that can be used in
expressions and statements.
-
C uses operators to represent arithmetic operations.
-
The following C Operators are listed in order of decreasing
precedence.
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)
-
an int value is used to representr Boolean values in
C language. long and unsigned.
-
a nonzero value is treated as true in C language.
-
a zero value is treated as false in C language.
-
examples
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 : ~
-
This unary operator changes each bit that is a 1 to a
0 and 0 to 1.
Bitwise AND : &
-
This binary operator produces a new value by making a
bit by bit comparison between the two operands. For each bit posigion the
resulting bit is a 1 only if both corresponding bits in the operands are
1.
Bitwise OR: |
-
This binary operator produces a new valuie by making
a bit-by-bit comparison between the two operands. For each bit position,
the resulting but is a 1 if either of the corresponding bits in the operands
is a bit 1.
Bitwise Exclusive OR: ^
-
this binary operator produces a new value by making a
bit-by-bit comparison between the two operands. For each bit position,
the resulting bit is a 1 if one or the other (but not both) of the
corresponding bits in the operands is a bit 1.
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
++ --
-
the increment operator (++) increments or increases
the value of its operand by one.
-
the decrement operator (--) decrements or decreases
the value of its operand by one.
-
there are twi varieties of the ++ and -- operator:
prefix mode:
pre-increment or pre-decrement
the ++ or -- is placed before the affected variable.
-
the variable's contents is incremented or decremented
before using its value in an expression.
example:
x = 2 * ++y;
-
in the example above, y is increment by 1 first, then
multiply y by 2 and assign the result to x.
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.
-
the variable's contents is incremented or decremented
before using its value in an expression.
example:
x = 2 * ++y;
-
in the example above, y is increment by 1 first, then
multiply y by 2 and assign the result to x after the affected variable.
-
the variable's contents is used first and is incremented
or decremented after the expression has been evaluated.
example:
x = 2 * y--;
-
in the example above, y multiplied by two first, and
the result is assigned to x, then y is decremented 1.
-
if the increment or decrement operator is used by itself
in a statement, for example:
y++;
or
++y;
-
there is no difference if the prefix or
postfix form is used, both will have the final effect of incrementing
or decrementing the variable by one.
Conditional expression operator:
? :
-
the shorthand way to express the if-else statement.
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)
-
this operator converts its operands value to the type
specified by the enclosed keyword.
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
-
operator precedence refers to which operator is
evaluated first in an expression. operators grouped together have the same
level of precedence. For example, the relational operators have the same
level of precedence but have a higher precedence over the assignment operators.
example :
a = b - c * d;
-
since the * operator has a higher precendence over the
- operator, c * d evaluated first, then this result is subtracted from
b.
-
operator associativity refers to which operator
is evaluated first in an expression that have two or more operators of
the same level of precedence.
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.
-
C languange allows for mixed data types in an expression.
-
These different data types in the expression are then
converted to be of the same type.
-
C uses a set of rules to convert these different types
automatically.
The rules are :
-
if char and short int (signed or unsigned)
are used in an expression, it is automatically converted to int.
-
a promotion of a data type occurs when a type
is converted to a larger type.
-
in any operation involvinig two different types, both
values are converted to the "higher" ranking of the two types.
-
The ranking of types are as follows (from highest to
lowest):
-
long double
-
double
-
float
-
unsigned long int
-
long int
-
unsigned long int
-
long int
-
unsigned int
-
int
-
in an assignment statement, the final result of the calculation
is converted to the type of the varialbe that is being assigned a value.
-
this process can result in a promotion or a demotion,
in which the value is converted to a lower ranking type.