 C supports a large set of built-in operators.
 An operator is a symbol that tells the compiler to
perform certain mathematical or logical
manipulations.
 Operators are used in program to manipulate data
and variables.
 C operators can be classified into following types
› Arithmetic operators
› Relation operators
› Logical operators
› Bitwise operators
› Assignment operators
› Conditional operators
› Special operators
 C supports all the basic arithmetic operators.
Operator Description
+ Adds two operands
- Subtracts second operand
from first
* Multiply two operand
/ Divide numerator by
denominator
% Remainder of division
++ Increment operator-
increases integer value by
one
-- Decrement operator-
decreases integer value by
one
#include<stdio.h> // Header File
#include <conio.h>
int b=10; //Global Declaration
void main ( ) /* main is the starting of every c program */
{
int a,c; //Local Declaration
clrscr( );
scanf(“%d”,&a);
printf(“ n The sum of the two values:”);
c = a+b;
printf(“%d”,c);
getch( );
}
 The relational operators are
Operator Description
== Check if two operand are
equal
!= Check if two operand are
not equal
> Check if operand on the
left is greater than
operand on the right
< Check if operand on the
left is smaller than
operand on the right
>= Check left operand is
greater than or equal to
right operand
<= Check if operand on left is
smaller than or equal to
right operand
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=10,b=3,c=5;
clrscr( );
if(a>b) // relational operator
{
printf(" n a is bigger than b");
}
if((a>b)&&(a>c)) //Logical operator
{
printf(" n a is biggest");
}
getch( );
}
Output:
a is bigger than b
a is biggest
 C supports following 3 logical operators
 Suppose a=1 and b=0
Operator Description Example
&& Logical AND (a&&b) is false
|| Logical OR (a||b) is false
! Logical NOT (!a) is false
#include<stdio.h>
int main()
{
int year;
printf("Enter any year: ");
scanf("%d",&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
return 0;
}
Sample output:
Enter any year: 2010
2010 is not a leap year
 Bitwise operators perform manipulations of data
at bit level.
 These operators also perform shifting of bits from
right to left
 Bitwise operators are not applied to float or
double.
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Left shift
>> Right shift
Truth table for bitwise &, | and ^
 The bitwise shift operator shifts the bit value.
 The left operand specifies the value to be shifted
and the right operand specifies the number of
positions that the bits in the value are to be
shifted.
 Both operands have the same precedence.
a b a&b a|b a^b
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Example:
a=0001000
b=2
a<<b=0100000
a>>b=0000010
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=5,b=4,c;
clrscr( );
c = a&b;
printf(" n value a&b is:%d",c);
getch( );
}
Output:
value a&b is:4
 Assignment operators supported by C are
Operator Description Example
= Assigns values
from right side
operator to left side
operator
a=b
+= Adds right operand
to the left operand
and assign the
result to left
a+=b is same as
a=a+b
-= Subtracts right
operand from the
left operand and
assign the result to
left operand
a-=b is same as
a=a-b
*= Multiply left
operand with the
right operand and
assign the result
to left operand
a=*b is same as
a=a*b
/= Divides left
operand with the
right operand and
assign the result
to left operand
a/=b is same as
a=a/b
%= Calculate
modulus using
two operands and
assign the result
to left operand
a%=b is same as
a=a%b
#include<stdio.h>
#include <conio.h>
int b=10;
void main ( )
{
int a=3,b=5;
clrscr( );
a+=b; // a= a+b
printf(" n The sum of the two values:%d",a);
getch( );
}
Output:
The sum of the two values:8
 It is used to Increment or decrement an
operand.
 Eg: ++x (Pre Increment),
x++ (Post Increment),
--x (Pre Decrement),
x-- (Post Decrement).
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=5;
clrscr( );
printf(" n Post increment Value:%d",a++);
printf(" n Pre increment Value:%d",++a);
printf(" n Pre decrement Value:%d",--a);
printf(" n Post decrement Value:%d",a--);
getch( );
}
Output:
Post increment Value:5
Pre increment Value:7
Pre decrement Value:6
Post decrement Value:6
 Conditional operator in C is called by two more names:
› Ternary operator
› ?:Operator
 It is actually the if condition that we use in C language
 By using conditional operator we turn if condition statement
into more simple line of code
 The syntax of a conditional operator is:
expression 1?expression 2:expression 3
Where,
 The question mark “?” in the syntax represents the if
part
 The first expression is used to check the condition
 If that condition is true, then the expression2 is
executed
 If that condition is false, then the expression 3 is
executed.
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=5,b=8,c;
clrscr( );
c = a>b?a:b; //Conditional operator
printf(" n The Larger Value is%d",c);
getch( );
}
Output:
The Larger Value is 8
 comma operator ( , )
 sizeof operator
 pointer operator (& , *) etc,.
#include<stdio.h>
#include <conio.h>
void main ( )
{
int c;
clrscr( );
printf(" n size of int is:%d",sizeof c);
getch( );
}
Output:
size of int is: 2
 An expression represents data item such as
variable, constant which are interconnected using
operators.
 Eg:
Expression C Expression
a + b + c a + b + c
a2+b2 a*a + b*b
 The arithmetic expressions evaluation are
carried out based on the precedence and
associativity.
 The evaluation are carried in two phases.
› First Phase: High Priority operators are
evaluated.
› Second Phase: Low Priority operators are
evaluated.
Precedence Operator
High * , / , %
Low + , -
 5 - 20/4 + 3*3 – 1
= 5 - 5 + 9 – 1
= 0 + 9 – 1
= 9 – 1
= 8
 5 – (20/4) + 3*(3 – 1)
= 5 - 5 + 3*2
= 5 - 5 + 6
= 6

Expressions using operator in c

  • 2.
     C supportsa large set of built-in operators.  An operator is a symbol that tells the compiler to perform certain mathematical or logical manipulations.  Operators are used in program to manipulate data and variables.
  • 3.
     C operatorscan be classified into following types › Arithmetic operators › Relation operators › Logical operators › Bitwise operators › Assignment operators › Conditional operators › Special operators
  • 4.
     C supportsall the basic arithmetic operators. Operator Description + Adds two operands - Subtracts second operand from first * Multiply two operand / Divide numerator by denominator % Remainder of division ++ Increment operator- increases integer value by one -- Decrement operator- decreases integer value by one
  • 5.
    #include<stdio.h> // HeaderFile #include <conio.h> int b=10; //Global Declaration void main ( ) /* main is the starting of every c program */ { int a,c; //Local Declaration clrscr( ); scanf(“%d”,&a); printf(“ n The sum of the two values:”); c = a+b; printf(“%d”,c); getch( ); }
  • 6.
     The relationaloperators are Operator Description == Check if two operand are equal != Check if two operand are not equal > Check if operand on the left is greater than operand on the right < Check if operand on the left is smaller than operand on the right >= Check left operand is greater than or equal to right operand <= Check if operand on left is smaller than or equal to right operand
  • 7.
    #include<stdio.h> #include <conio.h> void main( ) { int a=10,b=3,c=5; clrscr( ); if(a>b) // relational operator { printf(" n a is bigger than b"); } if((a>b)&&(a>c)) //Logical operator { printf(" n a is biggest"); } getch( ); } Output: a is bigger than b a is biggest
  • 8.
     C supportsfollowing 3 logical operators  Suppose a=1 and b=0 Operator Description Example && Logical AND (a&&b) is false || Logical OR (a||b) is false ! Logical NOT (!a) is false
  • 9.
    #include<stdio.h> int main() { int year; printf("Enterany year: "); scanf("%d",&year); if(((year%4==0)&&(year%100!=0))||(year%400==0)) printf("%d is a leap year",year); else printf("%d is not a leap year",year); return 0; } Sample output: Enter any year: 2010 2010 is not a leap year
  • 10.
     Bitwise operatorsperform manipulations of data at bit level.  These operators also perform shifting of bits from right to left  Bitwise operators are not applied to float or double.
  • 11.
    Operator Description & BitwiseAND | Bitwise OR ^ Bitwise exclusive OR << Left shift >> Right shift
  • 12.
    Truth table forbitwise &, | and ^  The bitwise shift operator shifts the bit value.  The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value are to be shifted.  Both operands have the same precedence. a b a&b a|b a^b 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0
  • 13.
  • 14.
    #include<stdio.h> #include <conio.h> void main( ) { int a=5,b=4,c; clrscr( ); c = a&b; printf(" n value a&b is:%d",c); getch( ); } Output: value a&b is:4
  • 15.
     Assignment operatorssupported by C are Operator Description Example = Assigns values from right side operator to left side operator a=b += Adds right operand to the left operand and assign the result to left a+=b is same as a=a+b -= Subtracts right operand from the left operand and assign the result to left operand a-=b is same as a=a-b
  • 16.
    *= Multiply left operandwith the right operand and assign the result to left operand a=*b is same as a=a*b /= Divides left operand with the right operand and assign the result to left operand a/=b is same as a=a/b %= Calculate modulus using two operands and assign the result to left operand a%=b is same as a=a%b
  • 17.
    #include<stdio.h> #include <conio.h> int b=10; voidmain ( ) { int a=3,b=5; clrscr( ); a+=b; // a= a+b printf(" n The sum of the two values:%d",a); getch( ); } Output: The sum of the two values:8
  • 18.
     It isused to Increment or decrement an operand.  Eg: ++x (Pre Increment), x++ (Post Increment), --x (Pre Decrement), x-- (Post Decrement).
  • 19.
    #include<stdio.h> #include <conio.h> void main( ) { int a=5; clrscr( ); printf(" n Post increment Value:%d",a++); printf(" n Pre increment Value:%d",++a); printf(" n Pre decrement Value:%d",--a); printf(" n Post decrement Value:%d",a--); getch( ); } Output: Post increment Value:5 Pre increment Value:7 Pre decrement Value:6 Post decrement Value:6
  • 20.
     Conditional operatorin C is called by two more names: › Ternary operator › ?:Operator  It is actually the if condition that we use in C language  By using conditional operator we turn if condition statement into more simple line of code
  • 21.
     The syntaxof a conditional operator is: expression 1?expression 2:expression 3 Where,  The question mark “?” in the syntax represents the if part  The first expression is used to check the condition  If that condition is true, then the expression2 is executed  If that condition is false, then the expression 3 is executed.
  • 22.
    #include<stdio.h> #include <conio.h> void main( ) { int a=5,b=8,c; clrscr( ); c = a>b?a:b; //Conditional operator printf(" n The Larger Value is%d",c); getch( ); } Output: The Larger Value is 8
  • 23.
     comma operator( , )  sizeof operator  pointer operator (& , *) etc,.
  • 24.
    #include<stdio.h> #include <conio.h> void main( ) { int c; clrscr( ); printf(" n size of int is:%d",sizeof c); getch( ); } Output: size of int is: 2
  • 25.
     An expressionrepresents data item such as variable, constant which are interconnected using operators.  Eg: Expression C Expression a + b + c a + b + c a2+b2 a*a + b*b
  • 26.
     The arithmeticexpressions evaluation are carried out based on the precedence and associativity.  The evaluation are carried in two phases. › First Phase: High Priority operators are evaluated. › Second Phase: Low Priority operators are evaluated.
  • 27.
    Precedence Operator High *, / , % Low + , -  5 - 20/4 + 3*3 – 1 = 5 - 5 + 9 – 1 = 0 + 9 – 1 = 9 – 1 = 8  5 – (20/4) + 3*(3 – 1) = 5 - 5 + 3*2 = 5 - 5 + 6 = 6