Unit III Intro
Unit III Intro
Increment and decrement, assignment, relational, logical, conditional, bitwise and other
operators
Unit –III
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. C language is rich in built-in operators and provides the following types of
operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other
operators one by one.
Arithmetic Operators
Following table shows all the arithmetic operators supported by C language. Assume
variable A holds 10 and variable B holds 20 then:
B % A will
% Modulus Operator and remainder of after an integer division
give 0
A++ will
++ Increments operator increases integer value by one
give 11
A-- will
-- Decrements operator decreases integer value by one
give 9
Operators and expressions
Increment and decrement, assignment, relational, logical, conditional, bitwise and other
operators
Relational Operators
Following table shows all the relational operators supported by C language. Assume
variable A holds 10 and variable B holds 20, then:
Logical Operators
Following table shows all the logical operators supported by C language. Assume
variable A holds 1 and variable B holds 0, then:
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |,
and ^ are as follows:
Operators and expressions
Increment and decrement, assignment, relational, logical, conditional, bitwise and other
operators
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
~A = 1100 0011
The Bitwise operators supported by C language are listed in the following table. Assume
variable A holds 60 and variable B holds 13, then:
Assignment Operators
There are following assignment operators supported by C language:
Operators Precedence in C
Operator precedence determines the grouping of terms in an expression. This affects how
an expression is evaluated. Certain operators have higher precedence than others; for
example, the multiplication operator has higher precedence than the addition operator.
For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Operators and expressions
Increment and decrement, assignment, relational, logical, conditional, bitwise and other
operators
Here, operators with the highest precedence appear at the top of the table, those with the
lowest appear at the bottom. Within an expression, higher precedence operators will be
evaluated first.
2.5
Operator precedence determines which operator is performed first in an expression with more than
one operators with different precedence. For example 10 + 20 * 30 is calculated as 10 + (20 * 30)
and not as (10 + 20) * 30.
Associativity is used when two operators of same precedence appear in an expression. Associativity
can be either Left to Right or Right to Left. For example ‘*’ and ‘/’ have same precedence and their
associativity is Left to Right, so the expression “100 / 10 * 10” is treated as “(100 / 10) * 10”.
Operators and expressions
Increment and decrement, assignment, relational, logical, conditional, bitwise and other
operators
Precedence and Associativity are two characteristics of operators that determine the evaluation
order of subexpressions in absence of brackets.
1) Associativity is only used when there are two or more operators of same precedence.
The point to note is associativity doesn’t define the order in which operands of a single operator are
evaluated. For example consider the following program, associativity of the + operator is left to
right, but it doesn’t mean f1() is always called before f2(). The output of following program is in-fact
compiler dependent. See this for details.
// is compiler dependent.
int x = 0;
int f1() {
x = 5;
return x;
int f2() {
x = 10;
return x;
int main() {
return 0;
Run on IDE
This is necessary, otherwise there won’t be any way for compiler to decide evaluation order of
expressions which have two operators of same precedence and different associativity. For example +
and – have same associativity.
Operators and expressions
Increment and decrement, assignment, relational, logical, conditional, bitwise and other
operators
Precedence of postfix ++ is more than prefix ++, their associativity is also different. Associativity of
postfix ++ is left to right and associativity of prefix ++. See this for examples.
4) Comma has the least precedence among all operators and should be used carefully For example
consider the following program, the output is 1. See this and this for more details.
#include<stdio.h>
int main()
int a;
a = 1, 2, 3; // Evaluated as (a = 1), 2, 3
printf("%d", a);
return 0;
Run on IDE
In Python, expression like “c > b > a” is treated as “a > b and b > c”, but this type of chaining doesn’t
happen in C. For example consider the following program. The output of following program is
“FALSE”.
#include <stdio.h>
int main()
// is left to right. Therefore the value becomes ((30 > 20) > 10)
Operators and expressions
Increment and decrement, assignment, relational, logical, conditional, bitwise and other
operators
// which becomes (1 > 20)
if (c > b > a)
printf("TRUE");
else
printf("FALSE");
return 0;
Note:
#include <stdio.h>
int main()
// is left to right. Therefore the value becomes ((30 > 20) > 10)
if (c > b > a)
printf("TRUE");
else
printf("FALSE");
return 0;
// PROGRAM 1
#include <stdio.h>
int main(void)
{
Operators and expressions
Increment and decrement, assignment, relational, logical, conditional, bitwise and other
operators
int arr[] = {10, 20};
int *p = arr;
++*p;
return 0;
// PROGRAM 2
#include <stdio.h>
int main(void)
int *p = arr;
*p++;
return 0;
// PROGRAM 3
#include <stdio.h>
int main(void)
int *p = arr;
Operators and expressions
Increment and decrement, assignment, relational, logical, conditional, bitwise and other
operators
*++p;
return 0;
The output of above programs and all such programs can be easily guessed by remembering
following simple rules about postfix ++, prefix ++ and * (dereference) operators
2) Precedence of postfix ++ is higher than both * and prefix ++. Associativity of postfix ++ is left to
right.
The expression ++*p has two operators of same precedence, so compiler looks for assoiativity.
Associativity of operators is right to left. Therefore the expression is treated as ++(*p). Therefore the
output of first program is “arr[0] = 11, arr[1] = 20, *p = 11“.
The expression *p++ is treated as *(p++) as the precedence of postfix ++ is higher than *. Therefore
the output of second program is “arr[0] = 10, arr[1] = 20, *p = 20“.
The expression *++p has two operators of same precedence, so compiler looks for assoiativity.
Associativity of operators is right to left. Therefore the expression is treated as *(++p). Therefore the
output of second program is “arr[0] = 10, arr[1] = 20, *p = 20“.
Please write comments if you find anything incorrect, or you want to share more information about
the topic discussed above.
Please see the following precedence and associativity table for reference.
DESCRIPTION
OPERATOR ASSOCIATIVITY
Prefix increment/decrement
++ — Unary plus/minus
* / % Multiplication/division/modulus left-to-right
+ – Addition/subtraction left-to-right
|| Logical OR left-to-right
= Assignment
+= -= Addition/subtraction assignment
*= /= Multiplication/division assignment