The document discusses various operators in the C programming language. It begins by defining C operators as symbols that are used to perform logical and mathematical operations. It then describes the different types of operators in C - arithmetic, assignment, relational, logical, bitwise, conditional (ternary), and increment/decrement operators. For each type of operator, it provides examples and an example program to demonstrate their usage.
In this document
Powered by AI
Introduction to C operators, types including arithmetic, assignment, relational, logical, and bitwise.
Description and examples of arithmetic operators including addition, subtraction, multiplication, and division.
Explanation of assignment operators with examples demonstrating simple and compound assignments.
Overview of relational operators used for comparison, with examples to illustrate equality checks.
Introduction to logical operators including AND, OR and NOT, with examples of their usage in conditions.
Description of bitwise operations and a comparison of bitwise AND, OR, NOT, XOR, left, and right shift.
Overview of conditional (ternary) operators in C with syntax and example demonstrating its function.
Explains increment and decrement operators with examples illustrating both pre and post operations.
C – Operatorsand Expressions
The symbols which are used to perform logical and mathematical
operations in a C program are called C operators.
These C operators join individual constants and variables to form
expressions.
Operators, functions, constants and variables are combined together to
form expressions.
Consider the expression A + B * 5. Where, +, * are operators, A, B are
variables, 5 is constant and A + B * 5 is an expression.
Types of C operators:
C language offers many types of operators. They are,
1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Bit wise operators
6. Conditional operators (ternary operators)
7. Increment/decrement operators
8. Special operators
Continue on types of C operators:
S.no Types of Operators Description
1 Arithmetic operators
These are used to perform
mathematical calculations like
addition, subtraction,
multiplication, division and
modulus
2 Assignment operators
These are used to assign
the values for the variables in
C programs.
2.
3 Relational operators
Theseoperators are used
to compare the value of two
variables.
4 Logical operators
These operators are used to
perform logical operations on
the given two variables.
5 Bit wise operators
These operators are used to
perform bit operations on given
two variables.
6
Conditional (ternary)
operators
Conditional operators return
one value if condition is true
and returns another value is
condition is false.
7
Increment/decrement
operators
These operators are used to
either increase or decrease the
value of the variable by one.
8 Special operators
&, *, sizeof () and ternary
operators.
C – Arithmetic Operators
Arithmetic Operators in C:
C Arithmetic operators are used to perform mathematical calculations
like addition, subtraction, multiplication, division and modulus in C
programs.
S.no Arithmetic Operators Operation Example
1 + Addition A+B
2 - Subtraction A-B
3 * multiplication A*B
4 / Division A/B
3.
5 % ModulusA%B
Example program for C arithmetic operators:
In this example program, two values “40″ and “20″ are used to perform
arithmetic operations such as addition, subtraction, multiplication,
division, modulus and output is displayed for each operation.
#include <stdio.h>
int main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %dn", add);
printf("Subtraction of a, b is : %dn", sub);
printf("Multiplication of a, b is : %dn", mul);
printf("Division of a, b is : %dn", div);
printf("Modulus of a, b is : %dn", mod);
}
Output:
Addition of a, b is : 60
Subtraction of a, b is : 20
4.
Multiplication of a,b is : 800
Division of a, b is : 2
Modulus of a, b is : 0
C – Assignment Operators
Assignment operators in C:
In C programs, values for the variables are assigned using assignment
operators.
For example, if the value “10″ is to be assigned for the variable “sum”,
it can be assigned as “sum = 10;”
Other assignment operators in C language are given below.
Operators Example Explanation
Simple
assignment
operator
= sum=10 10 is assigned to variable sum
Compound
assignment
operators
+= sum+=10 This_is_same_as_sum=sum+10…………
-= sum-=10 This is same as sum = sum-10
*= sum*=10 This is same as sum = sum*10
/+ sum/=10 This is same as sum = sum/10
%= sum%=10 This is same as sum = sum%10
&= sum&=10 This is same as sum = sum&10
^= sum^=10 This is same as sum = sum^10
Example program for C assignment operators:
In this program, values from 0 – 9 are summed up and total “45″ is
displayed as output.
Assignment operators such as “=” and “+=” are used in this program to
assign the values and to sum up the values.
5.
# include <stdio.h>
intmain()
{
int Total=0,i;
for(i=0;i<10;i++)
{
Total+=i; // This is same as Total = Toatal+i
}
printf("Total = %d", Total);
}
Output:
Total = 45
C – Relational Operators
Relational operators in C:
Relational operators are used to find the relation between two variables.
i.e. to compare the values of two variables in a C program.
S.no Operators Example Description
1 > x > y x is greater than y
2 < x < y x is less than y
3 >= x >= y
x is greater than or equal
to y
4 <= x <= y x is less than or equal to y
5 == x == y x is equal to y
6 != x != y x is not equal to y
6.
Example program forrelational operators in C:
In this program, relational operator (==) is used to compare 2 values
whether they are equal are not.
If both values are equal, output is displayed as ” values are equal”. Else,
output is displayed as “values are not equal”.
Note : double equal sign (==) should be used to compare 2 values. We
should not single equal sign (=).
#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");
}
}
Output:
m and n are not equal
C – Logical Operators
7.
Logical operators inC:
These operators are used to perform logical operations on the given
expressions.
There are 3 logical operators in C language. They are, logical AND (&&),
logical OR (||) and logical NOT (!).
S.no Operators Name Example Description
1 &&
logical
AND
(x>5)&&(y<5)
It returns
true when
both
conditions
are true
2 ||
logical
OR
(x>=10)||(y>=10)
It returns
true when
at-least one
of the
condition is
true
3 !
logical
NOT
!((x>5)&&(y<5))
It reverses
the state of
the operand
“((x>5) &&
(y<5))”
If “((x>5)
&& (y<5))”
is true,
logical NOT
operator
makes it
false
Example program for logical operators in C:
#include <stdio.h>
int main()
8.
{
int m=40,n=20;
int o=20,p=30;
if(m>n && m !=0)
{
printf("&& Operator : Both conditions are truen");
}
if (o>p || p!=20)
{
printf("|| Operator : Only one condition is truen");
}
if (!(m>n && m !=0))
{
printf("! Operator : Both conditions are truen");
}
else
{
printf("! Operator : Both conditions are true. "
"But, status is inverted as falsen");
}}
Output:
&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is
inverted as false
In this program, operators (&&, || and!) are used to perform logical
operations on the given expressions.
9.
&& operator– “if clause” becomes true only when both conditions
(m>n and m! =0) is true. Else, it becomes false.
|| Operator – “if clause” becomes true when any one of the condition
(o>p || p!=20) is true. It becomes false when none of the condition is
true.
! Operator – It is used to reverses the state of the operand.
If the conditions (m>n && m!=0) is true, true (1) is returned. This value
is inverted by “!” operator.
So, “! (m>n and m! =0)” returns false (0).
C – Bit wise Operators
Bit wise operators in C:
These operators are used to perform bit operations. Decimal values are
converted into binary values which are the sequence of bits and bit wise
operators work on these bits.
Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~
(bitwise OR), ^ (XOR), << (left shift) and >> (right shift).
Truth table for bit wise operation Bit wise
operators
x y x|y
x
&
y
x
^
y
Operator_symbol Operator_name
0 0 0 0 0 & Bitwise_AND
0 1 1 0 1 | Bitwise OR
1 0 1 0 1 ~ Bitwise_NOT
1 1 1 1 0 ^ XOR
<< Left Shift
>> Right Shift
Consider x=40 and y=80. Binary form of these values are given below.
x = 00101000
y= 01010000
All bit wise operations for x and y are given below.
10.
x&y = 00000000(binary) = 0 (decimal)
x|y = 01111000 (binary) = 120 (decimal)
~x =111111111111111111111111111111111111111111111111111111111
1010111
.. ..= -41 (decimal)
x^y = 01111000 (binary) = 120 (decimal)
x << 1 = 01010000 (binary) = 80 (decimal)
x >> 1 = 00010100 (binary) = 20 (decimal)
Note:
Bit wise NOT : Value of 40 in binary is
0000000000000000000000000000000000000000000000000000000000
101000. So, all 0′s are converted into 1′s in bit wise NOT operation.
Bit wise left shift and right shift : In left shift operation “x << 1 “, 1
means that the bits will be left shifted by one place. If we use it as “x <<
2 “, then, it means that the bits will be left shifted by 2 places.
Example program for bit wise operators in C:
In this example program, bit wise operations are performed as shown
above and output is displayed in decimal format.
#include <stdio.h>
int main()
{
int m=40,n=80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf("AND_opr value = %dn",AND_opr );
printf("OR_opr value = %dn",OR_opr );
printf("NOT_opr value = %dn",NOT_opr );
printf("XOR_opr value = %dn",XOR_opr );
printf("left_shift value = %dn", m << 1);
11.
printf("right_shift value =%dn", m >> 1);
}
Output:
AND_opr value = 0
OR_opr value = 120
NOT_opr value = -41
XOR_opr value = 120
left_shift value = 80
right_shift value = 20
C – Conditional Operators
Conditional or ternary operators in C:
Conditional operators return one value if condition is true and returns
another value is condition is false.
This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
Example : (A > 100 ? 0 : 1);
.
In above example, if A is greater than 100, 0 is returned else 1 is
returned. This is equal to if else conditional statements.
Example program for conditional/ternary operators in C:
#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %dn", x);
12.
printf("y value is%d", y);
}
Output:
x value is 1
y value is 2
C – Increment/decrement Operators
Increment operators are used to increase the value of the variable by one
and decrement operators are used to decrease the value of the variable by
one in C programs.
Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator : – - var_name; (or) var_name – -;
…
Example:
Increment operator: ++ I ; i ++ ;
Decrement operator: – - i ; i – - ;
…
Example program for increment operators in C:
In this program, value of “i” is incremented one by one from 1 up to 9
using “i++” operator and output is displayed as “1 2 3 4 5 6 7 8 9”.
//Example for increment operators
#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
13.
i++;
} }
Example programfor decrement operators in C:
In this program, value of “I” is decremented one by one from 20 up to
11 using “i–” operator and output is displayed as “20 19 18 17 16 15 14
13 12 11”.
//Example for decrement operators
#include <stdio.h>
int main()
{
int i=20;
while(i>10)
{
printf("%d ",i);
i--;
}
}
Output:
20 19 18 17 16 15 14 13 12 11
Difference between pre/post increment & decrement operators in C:
Below table will explain the difference between pre/post increment and
decrement operators in C.
S.no
Operator
type
Operator Description
14.
1
Pre
increment
++i
Value of iis incremented
before assigning it to
variable i.
2
Post-
increment
i++
Value of i is incremented
after assigning it to
variable i.
3
Pre
decrement
– –i
Value of i is decremented
before assigning it to
variable i.
4
Post
decrement
i– –
Value of i is decremented
after assigning it to
variable i.
Example program for pre – increment operators in C:
//Example for increment operators
#include <stdio.h>
int main()
{
int i=0;
while(++i < 5 )
{
printf("%d ",i);
}
return 0;
}
Step 1 : In above program, value of “i” is incremented from 0 to 1
using pre-increment operator.
Step 2 : This incremented value “1″ is compared with 5 in while
expression.
Step 3 : Then, this incremented value “1″ is assigned to the variable
“i”.
15.
Above 3steps are continued until while expression becomes false
and output is displayed as “1 2 3 4″.
Example program for post – increment operators in C:
#include <stdio.h>
int main()
{
int i=0;
while(i++ < 5 )
{
printf("%d ",i);
}
return 0;
}
Step 1 : In this program, value of i ”0″ is compared with 5 in while
expression.
Step 2 : Then, value of “i” is incremented from 0 to 1 using post-
increment operator.
Step 3 : Then, this incremented value “1″ is assigned to the variable
“i”.
Above 3 steps are continued until while expression becomes
false and output is displayed as “1 2 3 4 5″.
Example program for pre - decrement operators in C:
#include <stdio.h>
int main()
{
int i=10;
while(--i > 5 )
{
16.
printf("%d ",i);
}
return 0;
}
Step 1 : In above program, value of “i” is decremented from 10 to
9 using pre-decrement operator.
Step 2 : This decremented value “9″ is compared with 5 in while
expression.
Step 3 : Then, this decremented value “9″ is assigned to the
variable “i”.
Above 3 steps are continued until while expression becomes false
and output is displayed as “9 8 7 6″.
Example program for post - decrement operators in C:
#include <stdio.h>
int main()
{
int i=10;
while(i-- > 5 )
{
printf("%d ",i);
}
return 0;
}
Step 1 : In this program, value of i ”10″ is compared with 5 in while
expression.
Step 2 : Then, value of “i” is decremented from 10 to 9 using post-
decrement operator.
Step 3 : Then, this decremented value “9″ is assigned to the
variable “i”.
Above 3 steps are continued until while expression becomes
false and output is displayed as “9 8 7 6 5″.