0% found this document useful (0 votes)
59 views13 pages

Unit III Intro

The document discusses different types of operators in C language including arithmetic, relational, logical, bitwise, and assignment operators. It provides examples of each operator type using variables A and B. Operators allow mathematical and logical manipulations on operands. Common operators include those for addition, subtraction, comparison, AND, OR, assignment, and bit shifting.

Uploaded by

Shah Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views13 pages

Unit III Intro

The document discusses different types of operators in C language including arithmetic, relational, logical, bitwise, and assignment operators. It provides examples of each operator type using variables A and B. Operators allow mathematical and logical manipulations on operands. Common operators include those for addition, subtraction, comparison, AND, OR, assignment, and bit shifting.

Uploaded by

Shah Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Operators and expressions

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:

Operator Description Example


A + B will
+ Adds two operands
give 30
A - B will
- Subtracts second operand from the first
give -10
A * B will
* Multiplies both operands
give 200
B / A will
/ Divides numerator by de-numerator
give 2

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:

Operator Description Example


(A == B)
Checks if the values of two operands are equal or not, if yes
== is not
then condition becomes true.
true.
Checks if the values of two operands are equal or not, if values (A != B)
!=
are not equal then condition becomes true. is true.
(A > B) is
Checks if the value of left operand is greater than the value of
> not
right operand, if yes then condition becomes true.
true.
Checks if the value of left operand is less than the value of right (A < B) is
<
operand, if yes then condition becomes true. true.
(A >= B)
Checks if the value of left operand is greater than or equal to
>= is not
the value of right operand, if yes then condition becomes true.
true.
Checks if the value of left operand is less than or equal to the (A <= B)
<=
value of right operand, if yes then condition becomes true. is true.

Logical Operators
Following table shows all the logical operators supported by C language. Assume
variable A holds 1 and variable B holds 0, then:

Operator Description Example


(A &&
Called Logical AND operator. If both the operands are non-
&& B) is
zero, then condition becomes true.
false.
Called Logical OR Operator. If any of the two operands is non- (A || B)
||
zero, then condition becomes true. is true.
Called Logical NOT Operator. Use to reverses the logical state !(A &&
! of its operand. If a condition is true then Logical NOT operator B) is
will make false. true.

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&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~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:

Operator Description Example


(A & B) will
Binary AND Operator copies a bit to the result if it exists in give 12,
&
both operands. which is
0000 1100
(A | B) will
Binary OR Operator copies a bit if it exists in either give 61,
|
operand. which is
0011 1101
(A ^ B) will
Binary XOR Operator copies the bit if it is set in one give 49,
^
operand but not both. which is
0011 0001
(~A ) will
give -61,
Binary Ones Complement Operator is unary and has the
~ which is
effect of 'flipping' bits.
1100 0011
in 2's
Operators and expressions
Increment and decrement, assignment, relational, logical, conditional, bitwise and other
operators
complement
form.
A << 2 will
Binary Left Shift Operator. The left operands value is
give 240
<< moved left by the number of bits specified by the right
which is
operand.
1111 0000
A >> 2 will
Binary Right Shift Operator. The left operands value is
give 15
>> moved right by the number of bits specified by the right
which is
operand.
0000 1111

Assignment Operators
There are following assignment operators supported by C language:

Operator Description Example


C=A+B
Simple assignment operator, Assigns values from right side will assign
=
operands to left side operand value of A
+ B into C
C += A is
Add AND assignment operator, It adds right operand to the equivalent
+=
left operand and assign the result to left operand to C = C +
A
C -= A is
Subtract AND assignment operator, It subtracts right operand equivalent
-=
from the left operand and assign the result to left operand to C = C -
A
C *= A is
Multiply AND assignment operator, It multiplies right
equivalent
*= operand with the left operand and assign the result to left
to C = C *
operand
A
C /= A is
Divide AND assignment operator, It divides left operand with equivalent
/=
the right operand and assign the result to left operand to C = C /
A
C %= A is
Modulus AND assignment operator, It takes modulus using equivalent
%=
two operands and assign the result to left operand to C = C %
A
Operators and expressions
Increment and decrement, assignment, relational, logical, conditional, bitwise and other
operators
C <<= 2 is
<<= Left shift AND assignment operator same as C
= C << 2
C >>= 2 is
>>= Right shift AND assignment operator same as C
= C >> 2
C &= 2 is
&= Bitwise AND assignment operator same as C
=C&2
C ^= 2 is
^= bitwise exclusive OR and assignment operator same as C
=C^2
C |= 2 is
|= bitwise inclusive OR and assignment operator same as C
=C|2

Misc Operators ↦ sizeof & ternary


There are few other important operators including sizeof and ? : supported by C Language.

Operator Description Example


sizeof(a), where a is
sizeof() Returns the size of an variable.
integer, will return 4.
&a; will give actual address
& Returns the address of an variable.
of the variable.
*a; will pointer to a
* Pointer to a variable.
variable.
If Condition is true ? Then
?: Conditional Expression
value X : Otherwise value Y

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.

Category Operator Associativity


Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != 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 = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right

Interesting facts about Operator Precedence and Associativity in C

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.

// Associativity is not used in the below program. Output

// is compiler dependent.

int x = 0;

int f1() {

x = 5;

return x;

int f2() {

x = 10;

return x;

int main() {

int p = f1() + f2();

printf("%d ", x);

return 0;

Run on IDE

2) All operators with same precedence have same associativity

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

3) Precedence and associativity of postfix ++ and prefix ++ are different

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

5) There is no chaining of comparison operators in C

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()

int a = 10, b = 20, c = 30;

// (c > b > a) is treated as ((c > b) > a), associativity of '>'

// 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()

int a = 10, b = 20, c = 30;

// (c > b > a) is treated as ((c > b) > a), associativity of '>'

// is left to right. Therefore the value becomes ((30 > 20) > 10)

// which becomes (1 > 20)

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;

printf("arr[0] = %d, arr[1] = %d, *p = %d", arr[0], arr[1], *p);

return 0;

Ans: arr[0] = 11, arr[1] = 20, *p = 11

// PROGRAM 2

#include <stdio.h>

int main(void)

int arr[] = {10, 20};

int *p = arr;

*p++;

printf("arr[0] = %d, arr[1] = %d, *p = %d", arr[0], arr[1], *p);

return 0;

arr[0] = 10, arr[1] = 20, *p = 20

// PROGRAM 3

#include <stdio.h>

int main(void)

int arr[] = {10, 20};

int *p = arr;
Operators and expressions
Increment and decrement, assignment, relational, logical, conditional, bitwise and other
operators
*++p;

printf("arr[0] = %d, arr[1] = %d, *p = %d", arr[0], arr[1], *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

1) Precedence of prefix ++ and * is same. Associativity of both is right to left.

2) Precedence of postfix ++ is higher than both * and prefix ++. Associativity of postfix ++ is left to
right.

(Refer: Precedence Table)

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

() Parentheses (function call) (see Note 1)

[] Brackets (array subscript)


left-to-right
Operators and expressions
Increment and decrement, assignment, relational, logical, conditional, bitwise and other
operators
. Member selection via object name

-> Member selection via pointer

++ — Postfix increment/decrement (see Note 2)

Prefix increment/decrement

++ — Unary plus/minus

+– Logical negation/bitwise complement


!~
Cast (convert value to temporary value of type)
(type)
Dereference
*
Address (of operand)
&

sizeof Determine size in bytes on this implementation right-to-left

* / % Multiplication/division/modulus left-to-right

+ – Addition/subtraction left-to-right

<< >> Bitwise shift left, Bitwise shift right left-to-right

< <= Relational less than/less than or equal to

> >= Relational greater than/greater than or equal to left-to-right

== != Relational is equal to/is not equal to left-to-right

& Bitwise AND left-to-right

^ Bitwise exclusive OR left-to-right

| Bitwise inclusive OR left-to-right


Operators and expressions
Increment and decrement, assignment, relational, logical, conditional, bitwise and other
operators
&& Logical AND left-to-right

|| Logical OR left-to-right

?: Ternary conditional right-to-left

= Assignment

+= -= Addition/subtraction assignment

*= /= Multiplication/division assignment

%= &= Modulus/bitwise AND assignment

^= |= Bitwise exclusive/inclusive OR assignment

<<= >>= Bitwise shift left/right assignment right-to-left

Comma (separate expressions) left-to-right


,
It is good to know precedence and associativity rules, but the best thing is to use brackets,
especially for less commonly used operators (operators other than +, -, *.. etc). Brackets
increase readability of the code as the reader doesn’t have to see the table to find out the
order.

You might also like