2. Operator
An operator is a program element that is applied to one or
more operands in an expression or statement.
• Operands can be a value or a variable.
• Operators that take
• one operand Unary operators.
• two operands Binary operators.
• three operands Ternary operators.
Example:
5 + 7
Or,
a + b where a , b are two variables.
4. C Arithmetic Operators
Operator Purpose
+ Adds two operands
- Subtracts second operand from the first
* Multiplies two operands
/ Divides numerator by denominator
% Remainder after integer division
6. If one or both operands represent negative values,
then the addition, subtraction, multiplication and
division operations will result in values whose signs
are determined by the usual rules of algebra.
For remainder, most versions of C assign the sign of
the first operand to the remainder to fulfill the next
condition
dividend = (integer quotient)*divisor + remainder
i.e.
a = ((a/b)*b)+(a%b)
will always be satisfied.
8. Type Conversion
• The data type is promoted from lower to higher.
char
short
unsigned
short
int
unsigned
int
long
unsigned
long
long long
unsigned
long long
float
double
long
double
Example:
int i=7;
double d=5.5;
char c=‘w’;
Then the data type of
(i+c)-(2*d/5)
is double and the result is
123.8
9. Type Casting
• Explicit type conversion can be forced in any expression, with
a unary operator called a cast.
• Syntax: (type) expression
• Example: int n;
float x;
x=(float) n;
• The above statement will convert the value of n to a float
value before assigning to x, but n is not altered.
• Type casting doesn’t change the actual value of the variable
but the resultant value may be put in temporary storage.
10. Precedence(order of evaluation)
• The operators within C are grouped hierarchically according to
their precedence.
• Operations with a higher precedence are carried out before
operations having a lower precedence.
• Example:
2 precedence groups for arithmetic operators.
Group 1(higher precedence): *, /, %
Group 2(lower precedence): +, -
11. Associativity
• The order in which consecutive operations within the same
precedence group are carried out.
• Example:
each of the 2 precedence groups of arithmetic operators has
associativity from
Left Right
a – b / c * d
12. Associativity
• The order in which consecutive operations within the same
precedence group are carried out.
• Example:
each of the 2 precedence groups of arithmetic operators has
associativity from
Left Right
≡ a – [(b/c) * d]a – b / c * d
14. Unary Operators
• Operators that act upon a single operand to produce a new
value
• Same precedence group
• Associativity Right Left
Operator Purpose Example
- To negate a numerical constant,
variable or expression
-743, -0X7FFF,-0.2, -5E-8,
-var1, -(x+y), -3*(x+y)
++ Pre/Post increment operator ++a, a++
-- Pre/Post decrement operator --a, a--
sizeof() Returns size of its operand in bytes sizeof(int)
(type) Type casting (float) 5
16. Relational Operators
Operator Purpose
< Check if operand on the left is smaller than operand on the right
<= Check if operand on the let is smaller than or equal to right operand
> Check if operand on the left is greater than operand o the right
>= Check left operand is greater than or equal to right operand
• Same precedence group
• Associativity Left Right
17. Equality Operators
Operator Purpose
== Check if two operands are equal
!= Check if two operands are not equal
• Same precedence group
• Associativity Left Right
18. Equality Operators
Operator Purpose
== Check if two operands are equal
!= Check if two operands are not equal
• Same precedence group
• Associativity Left Right
The resulting value of both relational and equality operators is
either TRUE(integer 1) or FALSE(integer 0)
21. Logical Operators
Operator Purpose
&& Logical AND
|| Logical OR
!
(unary)
Logical NOT
• && has higher precedence than ||
• Associativity Left Right
24. Assignment Operators
Operator Purpose
= Assigns values from the right side operands to left side operand
+= a += 5 is same as a=a+5
-= a += 5 is same as a=a-5
*= a += 5 is same as a=a*5
/= a += 5 is same as a=a/5
%= a += 5 is same as a=a%5
25. Assignment Operators
• Same precedence group
• Associativity Right Left
• Example:
i = j = 5;
first assigns 5 to variable j, then assigns the value of j to i
27. Operator Purpose
~
(unary)
One’s compliment operator
(reverse each bits of operands i.e. 0 1 and 1 0)
& Bitwise AND
(perform and operations on each bits)
| Bitwise OR
(perform or operation on each bits)
^ Bitwise exclusive OR (XOR)
<< shift left
>> shift right
Bitwise Operators
• Associativity Left Right
• Precedence : shift > and > xor > or
28. Truth table for bitwise & , | , ^
a b a & b a | b a ^ b
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
29. Examples
a = 0x6db7 = 0110 1101 1011 0111
~a = 0x9248 = 1001 0010 0100 1000
b = 0xa726 = 1010 0111 0010 0110
~b = 0x58d9 = 0101 1000 1101 1001
a = 0x6db7 = 0110 1101 1011 0111
b = 0xa726 = 1010 0111 0010 0110
a & b = 0xca91 = 0010 0101 0010 0110
30. Examples
int a = 0x6db7 ; // 0110 1101 1011 0111
b = a << 6 ; // 0110 1101 1100 0000
int a = 0x6db7 ; // 0110 1101 1011 0111
b = a >> 6 ; // ???? ???? ???? ????
32. Bitwise Assignment Operators
Operator Purpose
&= a &= 0x7f equivalents to a= a & 0x7f
^= a ^= 0x7f equivalents to a= a ^ 0x7f
|= a |= 0x7f equivalents to a= a | 0x7f
<<= a <<= 5 equivalents to a= a << 5
>>= a >>= 5 equivalents to a= a >> 5
• Associativity Right Left
• Same precedence group
33. Conditional Operators
Operator
expression 1 ? expression 2 : expression 3
When evaluating expression, expression 1 is evaluated first.
If expression 1 is true, then expression 2 is evaluated and this
becomes the value of the conditional expression.
if expression 1 is false, then expression 3 is evaluated and this
becomes the value of the conditional expression.
35. Special operators
Operator Description
& Returns the address of an variable
Ex. Printf(“%#X n”, &x);
* Points to a memory location of specific data type
Ex. int *x;
, int a, b=10,c;
36. C operators precedence table
Category Operator Associativity
Postfix () [] -> . ++ -- L to R
Unary + - ! ~ ++ -- (type) * & sizeof() R to L
Multiplicative * / % L to R
Additive + - L to R
Shift << >> L to R
Relational < <= > >= L to R
Equality == != L to R
Bitwise AND & L to R
Bitwise XOR ^ L to R
Bitwise OR | L to R
Logical AND && L to R
Logical OR || L to R
Conditional ? : R to L
Assignment = += -= *= /= %= >>= <<= &= ^= |= R to L
Comma , L to R
37. References
• Schaum’s outlines “Programming with C” by Byron
Gottfried (3rd edition), Chapter 3 (full)
• https://www.programiz.com/c-programming/c-
operators
• https://www.tutorialspoint.com/cprogramming/c_oper
ators.htm