You are on page 1of 72

Operators in C

Operator

•An operator is a symbol that tells the compiler to perform


certain arithmetical or logical manipulation.
•They generally form a part of mathematical or logical
expression.

Operators in C Slide Number 2


Classification Criteria of Operators

•Number of Operands Required


•Type of Function the operator Performs

Operators in C Slide Number 3


Types of Operators

•On the basis of functions they performs, they are


•Arithmetic Operators
•Assignment Operators
•Increment and Decrement Operators
•Relational Operators

Operators in C Slide Number 4


Types of Operators …

•On the basis of functions they performs, they are …


• Logical Operators
• Conditional Operators
• Bitwise Operators
• Special Operators

Operators in C Slide Number 5


Types of Operators …

•On the basis of the number of operands they require to


perform, an operator is of following types
•Unary
•Binary
•Ternary
•Nary

Operators in C Slide Number 6


Expression

•An expression is the combination of constant(s), variable(s)


and operator(s) that reduces into single value.

Operators in C Slide Number 7


Statement

•A valid statement in C is nothing but an expression


terminated by semicolon i.e. ‘;’ sign.

Operators in C Slide Number 8


Types of Statement

•Depending on the type of job it dispenses or the type of


operator(s) involved, statement is of following types
•Declaration Statement
•Executable Statement

Operators in C Slide Number 9


Arithmetic Operators

•The arithmetic operators are


•Addition Operator i.e. ‘+’
•Subtraction Operator i.e. ‘-’
•Multiplication Operator i.e. ‘*’
•Division Operator i.e. ‘/’
•Modulus Division Operator i.e. ‘%’
•Arithmetic Operators are Binary in Nature.

Operators in C Slide Number 10


WAPC to illustrate the use of Arithmetic
Operator: Addition

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;

Operators in C Slide Number 11


WAPC to illustrate the use of Arithmetic
Operator: Addition …

int c;
c = a + b;
}

Operators in C Slide Number 12


WAPC to illustrate the use of Arithmetic
Operator: Subtraction

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;

Operators in C Slide Number 13


WAPC to illustrate the use of Arithmetic
Operator: Subtraction …

int c;
c = a - b;
}

Operators in C Slide Number 14


WAPC to illustrate the use of Arithmetic
Operator: Multiplication

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;

Operators in C Slide Number 15


WAPC to illustrate the use of Arithmetic
Operator: Multiplication …

int c;
c = a * b;
}

Operators in C Slide Number 16


WAPC to illustrate the use of Arithmetic
Operator: Division

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;

Operators in C Slide Number 17


WAPC to illustrate the use of Arithmetic
Operator: Division …

int c;
c = a / b;
}

Operators in C Slide Number 18


WAPC to illustrate the use of Arithmetic
Operator: Modulo Division

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;

Operators in C Slide Number 19


WAPC to illustrate the use of Arithmetic
Operator: Modulo Division …

int c;
c = a % b;
}

Operators in C Slide Number 20


Assignment Operators

•The assignment operator is ‘=’


•The operator is used to assign value to the variable.
•The syntax is variableName=value (constant or
variableName);

Operators in C Slide Number 21


WAPC to illustrate the use of Assignment
Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;

Operators in C Slide Number 22


WAPC to illustrate the use of Assignment
Operator …

int c;
a=5;
b=6;
c = a + b;
}

Operators in C Slide Number 23


Shorthand Assignment Operators

•Shorthand Addition Operator i.e. “+=” a+=1;


•Shorthand Subtraction Operator i.e. “-=” a-=1;
•Shorthand Multiplication Operator i.e. “*=” a*=2;
•Shorthand Division Operator i.e. “/=” a/=2;
•Shorthand Modulus Division Operator i.e. “%=” a%=3;

Operators in C Slide Number 24


Shorthand Assignment Operators:
Addition

•Shorthand Addition Operator i.e. “+=”


•int a=5;
•The example is a+=1;
•It is equivalent to a = a+ 1;
•The variable ‘a’ has value 5.

Operators in C Slide Number 25


Shorthand Assignment Operators:
Addition

•When the shorthand addition operator is implied onto ‘a’,


the new value of ‘a’ becomes 6 by adding 1 to the current
value and assigning the same to variable ‘a’.

Operators in C Slide Number 26


Shorthand Assignment Operators:
Subtraction

•Shorthand Addition Operator i.e. “-=”


•int a=5;
•The example is a-=1;
•It is equivalent to a = a- 1;
•The variable ‘a’ has value 4.

Operators in C Slide Number 27


Shorthand Assignment Operators:
Subtraction

•When the shorthand subtraction operator is implied onto


‘a’, the new value of ‘a’ becomes 4 by subtracting 1 to the
current value and assigning the same to variable ‘a’.

Operators in C Slide Number 28


Shorthand Assignment Operators:
Multiplication

•Shorthand Addition Operator i.e. “*=”


•int a=5;
•The example is a*=1;
•It is equivalent to a = a* 2;
•The variable ‘a’ has value 10.

Operators in C Slide Number 29


Shorthand Assignment Operators:
Multiplication

•When the shorthand multiplication operator is implied


onto ‘a’, the new value of ‘a’ becomes 10 by Multiplying
the current value of variable a by 2 and assigning the
product to variable ‘a’.

Operators in C Slide Number 30


Shorthand Assignment Operators: Division

•Shorthand Addition Operator i.e. “/=”


•int a=5;
•The example is a/=1;
•It is equivalent to a = a/ 2;
•The variable ‘a’ has value 2.

Operators in C Slide Number 31


Shorthand Assignment Operators: Division

•When the shorthand division operator is implied onto ‘a’,


the new value of ‘a’ becomes 2 by Dividing the current
value of variable a by 2 and assigning the quotient to
variable ‘a’.

Operators in C Slide Number 32


Shorthand Assignment Operators: Modulo
Division

•Shorthand Addition Operator i.e. “%=”


•int a=5;
•The example is a%=2;
•It is equivalent to a = a% 2;
•The variable ‘a’ has value 1.

Operators in C Slide Number 33


Shorthand Assignment Operators: Modulo
Division

•When the shorthand modulo division operator is implied


onto ‘a’, the new value of ‘a’ becomes 1 by Dividing the
current value of variable a by 2 and assigning the
remainder to variable ‘a’.

Operators in C Slide Number 34


WAPC to illustrate the use of Shorthand
Addition Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
a = 5;

Operators in C Slide Number 35


WAPC to illustrate the use of Shorthand
Addition Operator …

a += 5;
}

Operators in C Slide Number 36


WAPC to illustrate the use of Shorthand
Subtraction Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
a = 5;

Operators in C Slide Number 37


WAPC to illustrate the use of Shorthand
Subtraction Operator …

a -= 5;
}

Operators in C Slide Number 38


WAPC to illustrate the use of Shorthand
Multiplication Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
a = 5;

Operators in C Slide Number 39


WAPC to illustrate the use of Shorthand
Multiplication Operator …

a *= 5;
}

Operators in C Slide Number 40


WAPC to illustrate the use of Shorthand
Division Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
a = 5;

Operators in C Slide Number 41


WAPC to illustrate the use of Shorthand
Division Operator …

a /= 5;
}

Operators in C Slide Number 42


WAPC to illustrate the use of Shorthand
Modulo Division Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
a = 5;

Operators in C Slide Number 43


WAPC to illustrate the use of Shorthand
Modulo Division Operator …

a %= 5;
}

Operators in C Slide Number 44


Increment Operator

•The increment operator is ‘++’.


•Increases the value of Operand by 1.
•Unary in Nature.
•Requires only one Operand.
•The example is a++; Postfix
•The example is ++a; Prefix

Operators in C Slide Number 45


Decrement Operator

•The decrement operator is ‘++’.


•Decreases the value of Operand by 1.
•Unary in Nature.
•Requires only one Operand.
•The example is a--; Postfix
•The example is --a; Prefix

Operators in C Slide Number 46


WAPC to illustrate the use of Increment
Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
a = 5;

Operators in C Slide Number 47


WAPC to illustrate the use of Increment
Operator …

++a;/* Prefix use of Increment Operator*/


a++; /* Postfix use of Increment Operator*/
}

Operators in C Slide Number 48


WAPC to illustrate the use of Decrement
Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
a = 5;

Operators in C Slide Number 49


WAPC to illustrate the use of Decrement
Operator …

--a;/* Prefix use of Decrement Operator*/


a--; /* Postfix use of Decrement Operator*/
}

Operators in C Slide Number 50


Relational Operator

•The relational operators are


•Equal to operator i.e. “==”
•Less than operator i.e. “<”
•Greater than operator i.e. “>”
•Less that and equal to operator i.e. “<=”
•Greater than and equal to operator i.e. “>=”
•Not equal to operator i.e. “!=”

Operators in C Slide Number 51


WAPC to illustrate the use of Relational
Operator: Equal to i.e. “==”

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;

Operators in C Slide Number 52


WAPC to illustrate the use of Relational
Operator: Equal to i.e. “==” …

printf(“Enter an Two Integer Type Value”);


scanf(“%d %d”, &a,&b);
if(a==b)
{
printf(“\n%d and %d are equal”,a,b);
}
}
Operators in C Slide Number 53
WAPC to illustrate the use of Relational
Operator: Less Than i.e. “<”

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;

Operators in C Slide Number 54


WAPC to illustrate the use of Relational
Operator: Less Than i.e. “<” …

printf(“Enter an Two Integer Type Value”);


scanf(“%d %d”, &a,&b);
if(a<b)
{
printf(“\n%d < %d.”,a,b);
}
}
Operators in C Slide Number 55
WAPC to illustrate the use of Relational
Operator: Greater Than i.e. “>”

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;

Operators in C Slide Number 56


WAPC to illustrate the use of Relational
Operator: Greater Than i.e. “>” …

printf(“Enter an Two Integer Type Value”);


scanf(“%d %d”, &a,&b);
if(a>b)
{
printf(“\n%d > %d.”,a,b);
}
}
Operators in C Slide Number 57
WAPC to illustrate the use of Relational
Operator: Not Equal To i.e. “!=”

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;

Operators in C Slide Number 58


WAPC to illustrate the use of Relational
Operator: Not Equal To i.e. “!=” …

printf(“Enter an Two Integer Type Value”);


scanf(“%d %d”, &a,&b);
if(a!=b)
{
printf(“\n%d != %d.”,a,b);
}
}
Operators in C Slide Number 59
Logical Operator

•The logical operators are


•Logical Operator “AND” i.e. “&&”
•Logical Operator “OR” i.e. “||”
•Logical Operator “NOT” i.e. ‘!’

Operators in C Slide Number 60


WAPC to illustrate the use of Logical
Operator: “AND” i.e. “&&”

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;

Operators in C Slide Number 61


WAPC to illustrate the use of Logical
Operator: “AND” i.e. “&&” …

int c;
printf(“Enter an Three Integer Type Value”);
scanf(“%d %d %d”, &a,&b, &c);
scanf(“%d %d %d”, &a,&b, &c);

Operators in C Slide Number 62


WAPC to illustrate the use of Logical
Operator: “AND” i.e. “&&” …

if((a>b) && (a>c))


{
printf(“\n%d is greatest among %d, %d and %d
.”,a,a,b,c);
}
}

Operators in C Slide Number 63


WAPC to illustrate the use of Logical
Operator: “OR” i.e. “||”

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;

Operators in C Slide Number 64


WAPC to illustrate the use of Logical
Operator: “OR” i.e. “||” …

int c;
printf(“Enter an Three Integer Type Value”);
scanf(“%d %d %d”, &a,&b, &c);
scanf(“%d %d %d”, &a,&b, &c);

Operators in C Slide Number 65


WAPC to illustrate the use of Logical
Operator: “OR” i.e. “||” …

if((a>b) || (a>c))
{
printf(“\n%d is not the smallest among %d, %d
and %d.”,a,a,b,c);
}
}

Operators in C Slide Number 66


WAPC to illustrate the use of Logical
Operator: “NOT” i.e. “!”

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;

Operators in C Slide Number 67


WAPC to illustrate the use of Logical
Operator: “NOT” i.e. “!”

printf(“Enter an Two Integer Type Value”);


scanf(“%d %d”, &a,&b);
if(!(a>b))
{
printf(“\n%d is not greater than %d .”, a,b);
}
}
Operators in C Slide Number 68
Conditional Operator

•The conditional operator is “?:”

Operators in C Slide Number 69


WAPC to illustrate the use of Conditional
Operator: “?:”

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;

Operators in C Slide Number 70


WAPC to illustrate the use of Conditional
Operator: “?:” …

int c;
printf(“Enter an Two Integer Type Value”);
scanf(“%d %d”,&a,&b);
c=((a>b)?a:b);
printf(“\n%d is greater number .”, c);
}
}
Operators in C Slide Number 71
Bitwise Operator

•The bitwise operators are


•Bitwise Operator “AND” i.e. ‘&’
•Bitwise Operator “OR” i.e. ‘|’
•Bitwise Operator “NOT” i.e. ‘^’
•Bitwise Operator “Left Shift” i.e. ‘<<‘
•Bitwise Operator “Right Shift” i.e. ‘>>’

Operators in C Slide Number 72

You might also like