Operators & Expression
Operators & Expression
Module I
Topic – Operators and Expression
23ADT001 - C PROGRAMMING
OPERATORS AND EXPRESSIONS
Expression :
Combination of operators and operand
Operators:
An operator is a symbol that specifies an operation to
be performed on the operands.
Operand:
An operand is a data item on which operators perform
the operations.
23ADT001 - C PROGRAMMING
Types of operators
• Based on number of operands
• Unary operator
• Binary operator
• Ternary operator
23ADT001 - C PROGRAMMING
Based on the operation
Arithmetic operators
Relational operators
Logical operators
Increment and decrement operators
Bitwise operators
Assignment operators
Comma operator
Conditional operators
Special operators
23ADT001 - C PROGRAMMING
1. ARITHMETIC OPERATORS
- used for numerical calculations between two constant values
23ADT001 - C PROGRAMMING
UNARY OPERATOR
C Operation Operator Example
Positive + a = +3
Negative/Minus - b = -a
Increment ++ i++
Decrement -- i--
23ADT001 - C PROGRAMMING
BINARY OPERATORS
C Operation Operator Example:
Addition + a+3
Subtraction - a-6
Multiplication * a*b
Division / a/c
Modulus % a%x
• The division of variables of type int will always produce a variable of type int as the result.
• You could only use modulus (%) operation on int variables.
23ADT001 - C PROGRAMMING
TYPES OF OPERATORS
23ADT001 - C PROGRAMMING
• Output
a+b = 13
include <stdio.h>
a-b = 5
int main()
a*b = 36
{
a/b = 2 Remainder when a
int a = 9,b = 4, c; divided by b=1
c = a+b; printf("a+b = %d \n",c);
c = a-b; printf("a-b = %d \n",c);
c = a*b; printf("a*b = %d \n",c);
c=a/b; printf("a/b = %d \n",c);
c=a%b; printf("Remainder when a divided by b = %d \
n",c);
return 0;
} 23ADT001 - C PROGRAMMING
• In C, (note that all these example are using 9 as the
value of its first operand)
23ADT001 - C PROGRAMMING
2. Relational operator
Relational operators are used to distinguish between two values
depending on their relations.
If the relation is true then it returns a value 1, otherwise 0 for false
relation.
23ADT001 - C PROGRAMMING
RELATIONAL OPERATORS:
• The relational operators
enable you to check
whether two variables or
expressions are equal, not
equal, which one is
greater or less than other
etc.
23ADT001 - C PROGRAMMING
Operator Description Example
> Checks if the value of left operand is greater (A > B) is not true.
than the value of right operand, if yes then
condition becomes true.
< Checks if the value of left operand is less than (A < B) is true.
the value of right operand, if yes then condition
becomes true.
>= Checks if the value of left operand is greater (A >= B) is not true.
than or equal to the value of right operand, if
yes then condition becomes true.
<= Checks if the value of left operand is less than (A <= B) is true
or equal to the value of right operand, if yes
then condition becomes true. - C PROGRAMMING
23ADT001
// C Program to demonstrate the working of relational operators
#include <stdio.h>
int main() {
Output
int a = 5, b = 5, c = 10; 5 == 5 = 1
printf("%d == %d = %d \n", a, b, a == b); // true 5 == 10 = 0
printf("%d == %d = %d \n", a, c, a == c); // false 5>5=0
5 > 10 = 0
printf("%d > %d = %d \n", a, b, a > b); //false 5<5=0
printf("%d > %d = %d \n", a, c, a > c); //false 5 < 10 = 1
printf("%d < %d = %d \n", a, b, a < b); //false 5 != 5 = 0
5 != 10 = 1
printf("%d < %d = %d \n", a, c, a < c); //true 5 >= 5 = 1
printf("%d != %d = %d \n", a, b, a != b); //false 5 >= 10 =
0
printf("%d != %d = %d \n", a, c, a != c); //true
5 <= 5 = 1
printf("%d >= %d = %d \n", a, b, a >= b); //true 5 <= 10 = 1
printf("%d >= %d = %d \n", a, c, a >= c); //false
printf("%d <= %d = %d \n", a, b, a <= b); //true
printf("%d <= %d = %d \n", a, c, a 23ADT001
<= c);- C//true
PROGRAMMING
3. LOGICAL OPERATOR:
• You use C logical operators to
connect expressions and / or
variables to form compound
conditions.
• The C logical expression return
an integer (int).
• The result has value 1 if the
expression is evaluated to true !=
otherwise it return 0.
• C uses the following symbols
for the Boolean operations
AND, OR ,and NOT.
23ADT001 - C PROGRAMMING
Operator Description Example
23ADT001 - C PROGRAMMING
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) equals to %d \n", result);
result = (a == b) && (c < b); Output
printf("(a == b) && (c < b) equals to %d \n", result); a = 5, b = 5, c = 10
result = (a == b) || (c < b); (a == b) && (c > b) equals to 1
printf("(a == b) || (c < b) equals to %d \n", result); (a == b) && (c < b) equals to 0
result = (a != b) || (c < b);
(a == b) || (c < b) equals to 1
printf("(a != b) || (c < b) equals to %d \n", result);
(a != b) || (c < b) equals to 0
result = !(a != b);
!(a != b) equals to 1
printf("!(a == b) equals to %d \n", result);
result = !(a == b); !(a == b) equals to 0
printf("!(a == b) equals to %d \n", result);
return 0;
} 23ADT001 - C PROGRAMMING
Logical AND (&&):
• This operator is used to evaluate 2 conditions or expressions with relational
operators simultaneously.
• If both the expressions to the left and to the right of the logical operator is
true then the whole compound expression is true.
Example
a > b && x = = 10
Logical OR (||):
• The logical OR is used to combine 2 expressions or the condition evaluates
to true if any one of the 2 expressions is true.
Example
a!=20 || b==6
Logical NOT (!)
The logical not operator takes single expression and evaluates to true if the
expression is false and evaluates to false if the expression is true.
• In other words it just reverses the value of the expression.
For example
! (x >= y)
23ADT001 - C PROGRAMMING
4. INCREMENT AND DECREMENT OPERATOR:
• The increment operator ++ adds the value 1 to the current value of operand.
• The decrement operator – – subtracts the value 1 from the current value of operand
Suppose, a = 5 then,
C=++a; //c=6 ,a becomes 6
d=a++; //d=6,a becomes 7
E=--a; //E=6, a becomes 6
F=a--; //F=6,a becomes 5
23ADT001 - C PROGRAMMING
Example
#include <stdio.h>
int main()
{
int a = 10, b = 100; float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
23ADT001 - C PROGRAMMING
OUTPUT
Output
++a = 11
--b = 99
++c = 11.500000
++d = 99.500000
23ADT001 - C PROGRAMMING
5. BITWISE OPERATORS
• Bitwise operators are used in C
Bitwise Operator
programming to perform bit-level Meaning of
operations. operators
operators
• Those operators are used for testing,
complementing or shifting bits to the & Bitwise AND
right on left.
| Bitwise OR
• Bitwise operators may not be applied to
^ Bitwise exclusive OR
a float or double.
~ Bitwise complement
23ADT001 - C PROGRAMMING
Bitwise operator
Bitwise AND operator & Bitwise OR operator |
12 = 00001100 (In Binary) 12 = 00001100 (In Binary)
25 = 00011001 (In Binary) 25 = 00011001 (In Binary)
Bit Operation of 12 and 25 Bitwise OR Operation of 12 and 25
00001100 & 00001100 |
00011001 00011001
________ ________
00001000 = 8 (In decimal) 00011101 = 29 (In decimal)
23ADT001 - C PROGRAMMING
Bitwise operator
Bitwise XOR (exclusive OR) Bitwise complement operator ~
operator ^
35 = 00100011 (In Binary)
12 = 00001100 (In Binary)
Bitwise complement Operation of
25 = 00011001 (In Binary) 35
~ 00100011
Bitwise XOR Operation of 12 ________
and 25
11011100 = 220 (In decimal)
00001100
^ 00011001
________
00010101 = 21 (In decimal)
23ADT001 - C PROGRAMMING
Left Shift Operator Right Shift Operator
Y=N* 2S Y=N/ 2S
23ADT001 - C PROGRAMMING
EXAMPLE
#include<stdio.h>
int main()
Output:
{ x << 1 = 38
int x = 19; x >> 1 = 9
printf ("x << 1 = %d\n", x << 1);
printf ("x >> 1 = %d\n", x >> 1);
return 0;
19=0000000000010011
} 19<<1=0000000000100110
=38 (DECIMAL)
23ADT001 - C PROGRAMMING
23ADT001 - C PROGRAMMING
6. ASSIGNMENT OPERATORS
• Assignment operators are used to combine the '=' operator
with one of the binary arithmetic operators
• In the following slide, All operations starting from c = 9
23ADT001 - C PROGRAMMING
7. CONDITIONAL OR TERNARY OPERATOR:
• The conditional operator consists of 2 symbols the question mark (?) and
the colon (:)
The syntax for a ternary operator is as follows
exp1 ? exp2 : exp3
• The ternary operator works as follows
exp1 is evaluated first. If the expression is true then exp2 is evaluated & its
value becomes the value of the expression.
• If exp1 is false, exp3 is evaluated and its value becomes the value of the
expression. Note that only one of the expression is evaluated.
• For example
a = 10;
b = 15;
• x = (a > b) ? a : b
x=(10>15)?10:15
• X=15
Here x will be assigned to the value of b. The condition follows that the
expression is false therefore b is assigned to x.
23ADT001 - C PROGRAMMING
EXAMPLE
Syntax: condition? (expression1): (expression2);
#include <stdio.h>
#include <conio.h>
void main()
{
int a=6,b=7;
printf(“Greatest of two numbers is %d”,((a>b)?a:b));
getch();
}
Output:
Greatest of two numbers is 7
23ADT001 - C PROGRAMMING
8. SPECIAL OPERATORS
23ADT001 - C PROGRAMMING
The size of Operator:
• The operator size of gives the size of the data
type or variable in terms of bytes occupied in
the memory.
• The operand may be a variable, a constant or a
data type qualifier.
Example
int sum;
m = sizeof (sum);
23ADT001 - C PROGRAMMING
include <stdio.h>
int main() {
int a, e[10]; float b; double c; char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
printf("Size of integer type array having 10 elements = %lu bytes\n",
sizeof(e));
return 0; }
• Output
• Size of int = 2 bytes
• Size of float = 4 bytes
• Size of double = 8 bytes
• Size of char = 1 byte
• Size of integer type array having 10 elements = 40 bytes
23ADT001 - C PROGRAMMING
Properties of operators
• Precedence - If more than one operators are involved in an
expression, C language has a predefined rule of priority for the
operators. This rule of priority of operators is called operator
precedence.
• Associativity - if two operators of same precedence (priority) is
present in an expression, Associativity of operators indicate the
order in which they execute.
23ADT001 - C PROGRAMMING
Precedence and associative
23ADT001 - C PROGRAMMING
a=5
23ADT001 - C PROGRAMMING
23ADT001 - C PROGRAMMING
Example of precedence Example of associativity
• 1 == 2 != 3
• (1 > 2 + 3 && 4)
Here, operators == and != have same
This expression is equivalent to: precedence.
((1 > (2 + 3)) && 4) The associativity of both == and != is left to
right, i.e, the expression on the left is
• i.e, (2 + 3) executes first resulting into executed first and moves towards the right.
5 then, first part of the expression • Thus, the expression above is equivalent to
:
(1 > 5) executes resulting into 0 (false)
• ((1 == 2) != 3)
then, (0 && 4) executes resulting into 0 • i.e, (1 == 2) executes first resulting into 0
(false) (false) then, (0 != 3) executes resulting into
1 (true)
Output
Output
• 0 • 1
23ADT001 - C PROGRAMMING
C operators in order of precedence (highest to lowest). Their associativity indicates
in what order operators of equal precedence in an expression are applied.
23ADT001 - C PROGRAMMING
SUMMARY
Bitwise operators
Assignment operators
Conditional operators
Special operators
Precedence and associativity
23ADT001 - C PROGRAMMING
References
• Ashok N.Kamthane, Amit.N.Kamthane, “Programming in C”, 3rd
Edition, Pearson Education, 2015
• Ajay Mittal, “Programming in C-A Practical Approach”, 3rd Edition,
Pearson Education, 2010.
• Yashavant P.Kanetkar, “Let Us C”, 16th Edition, BPB Publications,
2018.
• PradipDey, ManasGhosh, “Computer Fundamentals and
Programming in C”, 2nd Edition, Oxford University Press, 2013.
23ADT001 - C PROGRAMMING
23ADT001 - C PROGRAMMING