Operators
Operators
4. Operators in C
2
Outline
An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions.
These C operators join individual constants and variables to form expressions.
C language is rich in built-in operators and provides the following types of operators:
Arithmetic Operators
Increment and Decrement Operators
Assignment Operators
Logical Operators
Relational Operators
Conditional Operator
Bitwise Operators
Special Operators
4
Arithmetic Operators
Arithmetic Operators are used to performing mathematical calculations like addition (+),
subtraction (-), multiplication (*), division (/) and modulus (%).
Arithmetic Operators: Example
#include<stdio.h>
5
int main(){
float f = 12.67;
printf("%f\n", f); //print float value
printf("%e\n", f); //print in scientific notation
int a = 67;
printf("%o\n", a); //print in octal format
printf("%x\n", a); //print in hex format
Increment and Decrement Operators are useful operators generally used to minimize the
calculation.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1.
These two operators are unary operators, meaning they only operate on a single operand.
○ ++x is same as x = x + 1 or x += 1
○ --x is same as x = x - 1 or x -= 1
Increment and decrement operators can be used only with variables. They can't be used
with constants or expressions.
int x = 1, y = 1;
++x; // valid
++5; // invalid - increment operator operating on a constant value
++(x+y); // invalid - increment operating on an expression
9
Prefix and Postfix Increment and Decrement
#include<stdio.h>
int main()
{
int x = 12, y = 1;
printf("Initial value of x = %d\n", x); // print the initial value of x
printf("Initial value of y = %d\n\n", y); // print the initial value of y
The postfix increment/decrement operator causes the current value of the variable to be
used in the expression, then the value is incremented or decremented. For example:
y = x++;
Here first, the current value of x is assigned to y then x is incremented.
Similarly, in the statement:
y = x--;
the current value of x is assigned to y then x is decremented.
14
#include<stdio.h>
int main()
{
int x = 12, y = 1;
printf("Initial value of x = %d\n", x); // print the initial value of x
printf("Initial value of y = %d\n\n", y); // print the initial value of y
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
16
Assignment operators: Example
#include <stdio.h>
int main()
{
int a = 5, c;
// Working of assignment operators
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
17
Relational Operators
#include <stdio.h>
int main()
{
int m=40, n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
return 0;
}
19
Classroom Assignment
WAP to find a larger number among two numbers input by the user (use
relational operator for the comparison).
Hints:
A =6
B=7
If (A>B) {
Pri…. A is greater.
}
Else { b is greater
20
Logical Operators
C provides three logical operators when we test more than one condition to make
decisions.
These are: && (meaning logical AND), || (meaning logical OR) and ! (meaning logical
NOT).
The logical AND operator (&&) returns the boolean value true if both operands are true and
returns false otherwise.
Syntax: operand1 && operand2
Truth table of AND operator is:
22
OR (II) operator
The logical OR operator (||) returns the boolean value true if either or both operands is true
and returns false otherwise.
Syntax: operand1 || operand2
Truth Table of OR operator is:
23
NOT (!) operator
#include <stdio.h>
int main() {
int m=40,n=20;
int a=20,p=30;
if (m>n && m !=0) {
printf("&& Operator : Both conditions are true\n"); }
if (a>p || p!=20) {
printf("|| Operator : Only one condition is true\n"); }
if (!(m>n && m !=0)) {
printf("! Operator : Both conditions are true\n"); }
else {
printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n"); }
return 0;
}
25
Conditional Operator
#include <stdio.h>
int main()
{
int age; // variable declaration
printf("Enter your age");
scanf("%d",&age); // taking user input for age variable
(age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting")); // conditional
operator
return 0;
}
Syntax:
(Text Expression)? statement1 : statement2;
26
Bitwise Operators
The bitwise operators are the operators used to perform the operations on the
data at the bit-level.
When we perform the bitwise operations, then it is also known as bit-level
programming.
It consists of two digits, either 0 or 1.
It is mainly used in numerical computations to make the calculations faster.
1
0000 0000 0000 0001
256 128 64 32 16 8 4 2 1
1/17/2022
27
Bitwise Operators in C
| Bitwise OR Operator
Bitwise AND operator is denoted by the single ampersand sign (&). Two
integer operands are written on both sides of the (&) operator.
If the corresponding bits of both the operands are 1, then the output of the
bitwise AND operation is 1; otherwise, the output would be 0.
x y x&y
0 0 0
0 1 0
1 0 0
1 1 1
29
Bitwise AND Operator
Checking for Odd and Even Numbers using Bitwise AND (&) Checking if a number is a power of 2
#include <stdio.h>
#include <stdlib.h> #include <stdio.h>
#include <stdlib.h>
x y x|y
0 0 0
0 1 1
1 0 1
1 1 1
32
Bitwise OR Operator
The ^ operator is bitwise XOR. The usual bitwise OR operator is inclusive OR.
XOR is true only if exactly one of the two bits is true.
Two operands are written on both sides of the exclusive OR operator. If the corresponding
bit of any of the operand is 1 then the output would be 1, otherwise 0.
x y x^y
0 0 0
0 1 1
1 0 1
1 1 0
34
Bitwise Exclusive OR
return 0;
}
36
Complement(~), Left Shift (<<), Right
Shift (>>)
1. The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand,
the second operand decides the number of places to shift.
2. The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first
operand, the second operand decides the number of places to shift.
3. The ~ (bitwise NOT) in C or C++ takes one number and inverts all bits of it.
37
Complement(~), Left Shift (<<), Right
Shift (>>)
}
38
Special Operators
39
Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and decides how an expression
is evaluated.
Certain operators have higher precedence than others;
for example, the multiplication operator has a higher precedence than the addition operator.
40
41
Any queries???