Programming For Problem Solving
Lecture Number :- 3
“Operators in C "
Delivered by:
Mr. Neeraj Garg
Dept. of Computer Science & Engineering
Swami Keshvanand Institute of Technology,
Management & Gramothan, Jaipur
Learning Objectives
Types of operators
List of operators in C
Conditional
Arithmetic
Relational
Logical
Assignment
Increment/decrement
Bitwise
Special operators
2
TYPES OF OPERATORS
Binary operators are those operators which require two operands
to perform operation.
Example: 10 + 20, 2 > 4, a & b
Unary operators are those operators which require one operand
to perform operation.
Example: ++4, -3, sizeof (int)
Ternary operator requires three operands to perform operation.
Example: operand 1? operand 2 : operand3;
3
LIST OF OPERATORS IN C
Operator Names Symbol Representation
Conditional (Ternary) ?:
Arithmetic +, -, /, *, %
Relational >, <, >=, <=, ==,!=
Logical &&, ||, !
Assignment =
Increment / Decrement ++ , --
Comma operator ,
Bitwise operator &, |, ^, ~, >>, <<
Special operator sizeof( )
4
CONDITIONAL OPERATOR
The symbol ?: represents conditional operator (Ternary operator ).
Syntax as follows:
TRUE
Expression1? Expression 2: Expression3;
FALSE
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
a>0? printf(“a is positive”) : printf(“a is negative”);
getch();
}
Output : a is positive 5
ARITHMETIC OPERATOR
Arithmetic operators are used to perform numeric calculations.
• Example: We have two variables x = 5 and y = 2, Results are:
Operator symbol Name Expression Expression Results
+ Addition x+y 5+2 7
- Subtraction x-y 5–2 3
* Multiplication x*y 5*2 10
/ Division x/y 5/2 2
% Modulo x%y 5%2 1
#include<stdio.h> Program
#include<conio.h>
void main( ) Output:
{ in x=5,y=2;
printf(“%d %d\n”, x+y, x-y); 7 3
printf(“%d %d \n”,, x*y, x/y); 10 2
printf(“%d”, x%y); 1
getch();
} 6
RELATIONAL OPERATOR
Relational operator is an operator that compares two values.
It is also called comparison operator and return either True (1)
or False (0).
• Example: We have two variables x = 5 and y = 2, Result are:
Operator Name Expression Expression Results
symbol
< Less than x<y 5<2 0
> Greater than x>y 5>2 1
<= Less than x <= y 5 <= 2 0
equal to
>= Greater than x >= y 5 >= 2 1
equal to
== Equal Equal x == y 5 == 2 0
!= Not Equal x != y 5 != 2 1
7
RELATIONAL OPERATOR(Cont.)
Write a C program to show the use of relational operators.
#include<stdio.h>
#include<conio.h> // header file declaration
void main( ) // starting main function
{
int x=5, y=2; Output:
printf(“%d\n”, x<y); 0
printf(“%d\n”, x>y); 1
printf(“%d\n”, x<=y); 0
printf(“%d\n”, x>=y); 1
printf(“%d\n”, x==y); 0
printf(“%d\n”, x!=y); 1
getch();
}
8
LOGICAL OPERATOR
Logical operators are used to combine two expressions. The
expression may be variables, constants and functions.
C language supports three logical operators AND (&&), OR
(| |) , NOT(!)
#include<stdio.h>
A B A && B A || B !A #include<conio.h>
void main( )
{
0 0 0 0 1 int a=10, b=20;
int res1, res2;
0 1 0 1 1 res1 = ( a>=10 && b==20 );
res2 = ( a>=10 || b==20 );
1 0 0 1 0 printf(“Res1:= %d”, res1);
printf(“\nRes2: =%d”, res2);
getch();
1 1 1 1 0
}
Output: Res1:=1
Truth Table Res2:= 1
9
ASSIGNMENT OPERATOR
Assignment operator is used to assign the result of an
expression (Right hand side) to a variable (Left hand side).
The equal sign ( = ) is the Assignment operator in C.
Variable Name = value/constant/expression
(L-value) (r-value)
Short Hand Assignment
int A, B; // integer variable declaration (Compound operator)
A = 10; // variable initialization
a = a+1 a+=1
B = 20; // variable initialization
float C; // floating variable declaration b = b-1 b-=1
C = 2.75; //variable initialization
char ch; // character variable declaration c = c+(x*5) c+=(x*5)
ch = „M‟; // variable initialization
printf(“%d %d %f %c”, A, B, C, ch); p = p/10 p/=10
x = x%10 x%=10
Output: 10 20 2.75 M
10
INCREMENT/DECREMENT OPERATOR
Increment (++) operator increases the value of the variable by 1.
Decrement (--) operator decreases the value of the variable by1.
These operators are used either Post or Pre form.
Both are Unary operators.
Example:
Post Increment (X++) Pre Increment (++ X)
X = 10; X = 10;
Y = X++; X=?, Y=? Y = ++X; X=?, Y=?
Y = X; Y = 10 X=X+1; X=10+1=11
X = X + 1; X = 10 + 1 = 11 Y=X; Y=11
Result : X = 11 and Y = 10 Result: X =11 and Y =11
11
DECREMENT OPERATOR
Example :
Post Decrement (X--) Pre Decrement (-- X)
X = 10; X = 10;
Y = X--; X=?, Y=? Y = --X; X=?, Y=?
Y = X; Y = 10 X = X-1; X = 10 -1 = 9
X = X - 1; X = 10 - 1 = 9 Y = X; Y = 9
Result : X = 9 and Y = 10 Result: X = 9 and Y = 9
12
PROGRAM USING ++ AND --
Write a C Program to demonstrate ++ and -- operator.
#include <stdio.h> #include<stdio.h>
void main ( )
{
int X, Y;
X = 10;
Y = ++ X;
-- X;
Y --;
X = Y ++;
Y = -- X;
X = Y ++;
printf(“X=%d Y=%d”, X, Y);
getch();
}
Output: X = ? Y=? 13
PROGRAM USING ++ AND --
Write a C Program to demonstrate ++ and -- operator.
#include <stdio.h> // header file
#include<conio.h>
void main ( )
{
int X, Y; // Variable declarations
X = 10; // Variable initialization
Y = ++ X; // X=X+1=>10+1=>11 and Y=X=>11
-- X; // X=X-1=>11-1=>10
Y --; // Y=Y-1=>11-1=>10
X = Y ++; // X=Y=>10 and Y=Y+1=>10+1=>11
Y = -- X; // X=X-1=>10-1=>9 and Y=X=>9
X = Y ++; // X=Y=>9 and Y=Y+1=>9+1=>10
printf(“X=%d Y=%d”, X, Y);
}
Output: X = 9 Y=10 14
BITWISE OPERATOR
Bitwise operators are used to perform operation at bit level.
These operators are applied only integer data. (Not for float
and double) .
Example: Variables X = 11 and Y = 5
Binary equivalent: X 1011 and Y 0101
Symbol Name Expression Expression Calculation Result
& AND X &Y 11 & 5 1011 & 0101 0001
| OR X|Y 11 | 5 1011 | 0111 1111
^ XOR X^Y 11 ^ 5 1011 ^ 0101 1110
~ NOT ~X ~ 11 ~1011 0100
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
15
BITWISE OPERATOR(Cont.)
Left shift (<<) operator shifts the bit pattern to the left side.
It is written as x<<num; which means shifting the bits of x towards
left by num number of times.
Example: int x = 42;
int result = x<<1; result=?
Binary representation of 42 in 8 bits: 0010 1010
0 insert
Discard 0 0 1 0 1 0 1 0
0 1 0 1 0 1 0 0
Result: 84
16
BITWISE OPERATOR(Cont.)
Right shift (>>) operator shifts the bit pattern to the right side.
It is written as x>>num; which means shifting the bits of x towards
right by num number of times.
Example: int x = 42;
int result = x>>1; result=?
Binary representation of 42 in 8 bits: 0010 1010
0 0 1 0 1 0 1 0 Discard
insert
0 if num is +ve
and 1 if -ve
0 0 0 1 0 1 0 1
Result: 21
17
SPECIAL OPERATORS
C Supports Some special operators:
Operator Name Symbols #include<stdio.h>
#include<conio.h>
Comma , void main( )
{
int a=10;
Size of sizeof (variablename) float b=20.45;
char ch=„A‟;
prinf(“%d\n”, sizeof(a));
Pointer & and * printf(“%d\n”, sizeof(b));
Operators printf(“d\n”, sizeof(ch));
Member . And -> getch();
selection }
operator Output:
2
4
1
18
EXAMPLE
Write a C Program to find maximum of two numbers.
#include<stdio.h> // header file importing
#include<conio.h>
void main( )
{
int a, b; // integer variable declaration
printf(“Enter the two numbers a and b: \n”);
scanf(“%d%d”, &a,&b); // Read value a and b at run time
a>b? printf(“a is maximum”): printf(“b is maximum”);
getch();
}
Input: Enter the two numbers a and b:
100
200
Output:
b is maximum
19
EXAMPLE
Write a C Program to check a number is even or odd.
#include<stdio.h> // header file importing
#include<conio.h>
void main( ) // main function
{ // variable declaration
int a;
printf(“Enter the number : \n”); // printf function
scanf(“%d”, &a); // Read value of a
a%2 == 0? printf(“a is Even”) : printf(“a is Odd”);
getch();
}
Input: Enter the number :
21
Output: a is odd
20
PRACTICE PROGRAMS
1.Write a program to check a number is positive or negative.
2. Write a program to find maximum among three numbers.
3. Write a program to enter a four digit number and find out sum of its
digit using % and / operators.
[Example:1234= 1+2+3+4=>10]
4. Mahesh‟s basic salary is input through the keyboard. His dearness
allowance is 40% of basic salary , and house rent allowance is 20%
of basic salary. Write a program to calculate his gross salary.
[Example: Gross salary= Basic + DA+HRA]
5. The Distance is input through the keyboard. Write a program to
convert and print this distance in meters, feet, inches and
centimeters.
21
Thank You
22