Programming For Problem Solving
“Operators in C "
2
Learning Objectives
 Types of operators
 List of operators in C
 Conditional
 Arithmetic
 Relational
 Logical
 Assignment
 Increment/decrement
 Bitwise
 Special operators
3
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;
4
LIST OF OPERATORS IN C
Operator Names Symbol Representation
Conditional (Ternary) ? :
Arithmetic +, -, /, *, %
Relational >, <, >=, <=, ==,!=
Logical &&, ||, !
Assignment =
Increment / Decrement ++ , --
Comma operator ,
Bitwise operator &, |, ^, ~, >>, <<
Special operator sizeof( )
CONDITIONAL OPERATOR
The symbol ?: represents conditional operator (Ternary operator ).
Syntax as follows:
Expression1? Expression 2: Expression3;
5
TRUE
FALS
E
: printf(“a is negative”);
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
a>0? printf(“a is positive”)
getch();
}
Output : a is positive
ARITHMETIC OPERATOR
 Arithmetic operators are used to perform numeric
calculations.
• Example: We have two variables x = 5 and y = 2, Results are:
Program
6
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
Output:
#include<stdio.h>
#include<conio.h>
void main( )
{ in x=5,y=2;
printf(“%d %d
n”, x+y, x-y);
printf(“%d %d n”,, x*y, x/y);
7 3
10
2
1
printf(“%d”, x%y);
getch();
}
7
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
symbol
Name Expression Expression Results
< Less than x < y 5 < 2 0
> Greater than x > y 5 > 2 1
<= Less than
equal to
x <= y 5 <= 2 0
>= Greater than
equal to
x >= y 5 >= 2 1
= = Equal Equal x = = y 5 = = 2 0
!= Not Equal x != y 5 != 2 1
8
RELATIONAL OPERATOR(Cont.)
Write a C program to show the use of relational operators.
#include<stdio.h>
#include<conio.h>
void main( )
{
int x=5, y=2;
printf(“%dn”, x<y);
printf(“%dn”, x>y);
printf(“%dn”, x<=y);
printf(“%dn”,
x>=y); printf(“%d
n”, x==y);
printf(“%dn”, x!=y);
getch();
}
// header file declaration
// starting main function
Output:
0
1
0
1
0
1
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(!)
A B A && B A || B ! A
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0
Truth Table
#include<stdio.h>
#include<conio.h>
void main( )
{
int a=10, b=20;
int res1, res2;
res1 = ( a>=10 && b==20 );
res2 = ( a>=10 || b==20 );
printf(“Res1:= %d”, res1);
printf(“nRes2: =%d”, res2);
getch();
}
Output:
Res1:=1Res2:= 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
(Compound operator)
a = a+1 a+=1
b = b-1 b-=1
c = c+(x*5) c+=(x*5)
p = p/10 p/=10
x = x%10 x%=10
int A, B;
A = 10;
B = 20;
float C;
C = 2.75;
char ch;
ch = 'M‘;
10
// integer variable declaration
// variable initialization
// variable initialization
// floating variable declaration
//variable initialization
// character variable declaration
// variable initialization
printf(“%d %d %f %c”, A, B, C,
ch);
Output: 10 20 2.75 M
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;
11
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
DECREMENT OPERATOR
Pre Decrement (-- X)
X = 10;
Y = --X; X=?, Y=?
X = X-1; X = 10 -1 = 9 Y =
X; Y = 9
Result: X = 9 and Y = 9
 Example :
Post Decrement (X--)
X = 10;
Y = X--; X=?, Y=?
Y = X; Y = 10;
X = X - 1; X = 10 - 1 = 9
Result : X = 9 and Y =
10
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();
} 13
PROGRAM USING ++ AND --
Write a C Program to demonstrate ++ and -- operator.
14
#include <stdio.h>
#include<conio.h>
void main ( )
{
// header file
// Variable declarations
// Variable initialization
// X=X+1=>10+1=>11 and Y=X=>11
// X=X-1=>11-1=>10
// Y=Y-1=>11-1=>10
// X=Y=>10 and Y=Y+1=>10+1=>11
// X=X-1=>10-1=>9 and Y=X=>9
int X, Y;
X = 10;
Y = ++
X;
-- X;
Y --;
X = Y +
+; Y = --
X; X = Y
++;
// X=Y=>9 and Y=Y+1=>9+1=>10
printf(“X=%d Y=%d”, X, Y);
}
Output: X = 9 Y=10
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
15
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
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=?
0 0 1 0 1 0 1 0
 Binary representation of 42 in 8 bits: 0010 1010
0 insert
Discard
0 1 0 1 0 1 0 0
16
Result: 84
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
Operator Name Symbols
Comma ,
Size of sizeof
(variablename)
Pointer
Operators
& and *
Member
selection
operator
. And
->
 C Supports Some special operators:
#include<stdio.h>
#include<conio.h>
void main( )
{
int a=10;
float b=20.45;
char ch= ‘A
’;
printf(“%dn”,
sizeof(a));
printf(“%dn”,
sizeof(b));
1
18
sizeof(ch));
printf(“dn”,
getch();
}
Output:
2
4
EXAMPLE
Write a C Program to find maximum of two numbers.
// header file importing
19
#include<stdio.h>
#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
20
EXAMPLE
Write a C Program to check a number is even or odd.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a;
// header file importing
// main function
// variable declaration
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
21
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.
22
Thank You

Operators in C programming language.pptx

  • 1.
    Programming For ProblemSolving “Operators in C "
  • 2.
    2 Learning Objectives  Typesof operators  List of operators in C  Conditional  Arithmetic  Relational  Logical  Assignment  Increment/decrement  Bitwise  Special operators
  • 3.
    3 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;
  • 4.
    4 LIST OF OPERATORSIN C Operator Names Symbol Representation Conditional (Ternary) ? : Arithmetic +, -, /, *, % Relational >, <, >=, <=, ==,!= Logical &&, ||, ! Assignment = Increment / Decrement ++ , -- Comma operator , Bitwise operator &, |, ^, ~, >>, << Special operator sizeof( )
  • 5.
    CONDITIONAL OPERATOR The symbol?: represents conditional operator (Ternary operator ). Syntax as follows: Expression1? Expression 2: Expression3; 5 TRUE FALS E : printf(“a is negative”); #include<stdio.h> #include<conio.h> void main() { int a=10; a>0? printf(“a is positive”) getch(); } Output : a is positive
  • 6.
    ARITHMETIC OPERATOR  Arithmeticoperators are used to perform numeric calculations. • Example: We have two variables x = 5 and y = 2, Results are: Program 6 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 Output: #include<stdio.h> #include<conio.h> void main( ) { in x=5,y=2; printf(“%d %d n”, x+y, x-y); printf(“%d %d n”,, x*y, x/y); 7 3 10 2 1 printf(“%d”, x%y); getch(); }
  • 7.
    7 RELATIONAL OPERATOR  Relationaloperator 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 symbol Name Expression Expression Results < Less than x < y 5 < 2 0 > Greater than x > y 5 > 2 1 <= Less than equal to x <= y 5 <= 2 0 >= Greater than equal to x >= y 5 >= 2 1 = = Equal Equal x = = y 5 = = 2 0 != Not Equal x != y 5 != 2 1
  • 8.
    8 RELATIONAL OPERATOR(Cont.) Write aC program to show the use of relational operators. #include<stdio.h> #include<conio.h> void main( ) { int x=5, y=2; printf(“%dn”, x<y); printf(“%dn”, x>y); printf(“%dn”, x<=y); printf(“%dn”, x>=y); printf(“%d n”, x==y); printf(“%dn”, x!=y); getch(); } // header file declaration // starting main function Output: 0 1 0 1 0 1
  • 9.
    LOGICAL OPERATOR  Logicaloperators are used to combine two expressions. The expression may be variables, constants and functions.  C language supports three logical operators AND (&&), OR (| |) , NOT(!) A B A && B A || B ! A 0 0 0 0 1 0 1 0 1 1 1 0 0 1 0 1 1 1 1 0 Truth Table #include<stdio.h> #include<conio.h> void main( ) { int a=10, b=20; int res1, res2; res1 = ( a>=10 && b==20 ); res2 = ( a>=10 || b==20 ); printf(“Res1:= %d”, res1); printf(“nRes2: =%d”, res2); getch(); } Output: Res1:=1Res2:= 1 9
  • 10.
    ASSIGNMENT OPERATOR  Assignmentoperator 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 (Compound operator) a = a+1 a+=1 b = b-1 b-=1 c = c+(x*5) c+=(x*5) p = p/10 p/=10 x = x%10 x%=10 int A, B; A = 10; B = 20; float C; C = 2.75; char ch; ch = 'M‘; 10 // integer variable declaration // variable initialization // variable initialization // floating variable declaration //variable initialization // character variable declaration // variable initialization printf(“%d %d %f %c”, A, B, C, ch); Output: 10 20 2.75 M
  • 11.
    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; 11 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
  • 12.
    DECREMENT OPERATOR Pre Decrement(-- X) X = 10; Y = --X; X=?, Y=? X = X-1; X = 10 -1 = 9 Y = X; Y = 9 Result: X = 9 and Y = 9  Example : Post Decrement (X--) X = 10; Y = X--; X=?, Y=? Y = X; Y = 10; X = X - 1; X = 10 - 1 = 9 Result : X = 9 and Y = 10 12
  • 13.
    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(); } 13
  • 14.
    PROGRAM USING ++AND -- Write a C Program to demonstrate ++ and -- operator. 14 #include <stdio.h> #include<conio.h> void main ( ) { // header file // Variable declarations // Variable initialization // X=X+1=>10+1=>11 and Y=X=>11 // X=X-1=>11-1=>10 // Y=Y-1=>11-1=>10 // X=Y=>10 and Y=Y+1=>10+1=>11 // X=X-1=>10-1=>9 and Y=X=>9 int X, Y; X = 10; Y = ++ X; -- X; Y --; X = Y + +; Y = -- X; X = Y ++; // X=Y=>9 and Y=Y+1=>9+1=>10 printf(“X=%d Y=%d”, X, Y); } Output: X = 9 Y=10
  • 15.
    BITWISE OPERATOR  Bitwise operatorsare 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 15 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
  • 16.
    BITWISE OPERATOR(Cont.)  Leftshift (<<) 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=? 0 0 1 0 1 0 1 0  Binary representation of 42 in 8 bits: 0010 1010 0 insert Discard 0 1 0 1 0 1 0 0 16 Result: 84
  • 17.
    BITWISE OPERATOR(Cont.)  Rightshift (>>) 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
  • 18.
    SPECIAL OPERATORS Operator Name Symbols Comma, Size of sizeof (variablename) Pointer Operators & and * Member selection operator . And ->  C Supports Some special operators: #include<stdio.h> #include<conio.h> void main( ) { int a=10; float b=20.45; char ch= ‘A ’; printf(“%dn”, sizeof(a)); printf(“%dn”, sizeof(b)); 1 18 sizeof(ch)); printf(“dn”, getch(); } Output: 2 4
  • 19.
    EXAMPLE Write a CProgram to find maximum of two numbers. // header file importing 19 #include<stdio.h> #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
  • 20.
    20 EXAMPLE Write a CProgram to check a number is even or odd. #include<stdio.h> #include<conio.h> void main( ) { int a; // header file importing // main function // variable declaration 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
  • 21.
    21 PRACTICE PROGRAMS 1.Write aprogram 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.
  • 22.

Editor's Notes

  • #13 #include <stdio.h> void main ( ) { int X, Y; X = 10; Y = ++ X;// first increase than assign to y printf("X=%d Y=%d \n", X, Y); -- X;printf("X=%d Y=%d \n", X, Y); Y --;// first assign than decrease printf("X=%d Y=%d \n", X, Y); X = Y ++; printf("X=%d Y=%d \n", X, Y); Y = -- X; printf("X=%d Y=%d \n", X, Y); X = Y ++; // Post increment first assign than increase printf("X=%d Y=%d \n", X, Y); }
  • #14 // Online C compiler to run C program online#include <stdio.h>int main(){ int X, Y; X = 10;Y = ++ X;// pre increment first increment than assignprintf("X=%d Y=%d \n", X, Y);-- X;// pre Decrement decrement printf("X=%d Y=%d \n", X, Y);Y --;// post decrement Decrement printf("X=%d Y=%d \n", X, Y);X = Y ++; // Post increment first assign than increment printf("X=%d Y=%d \n", X, Y);Y = -- X;// pre Decrement first decrement than assignprintf("X=%d Y=%d \n", X, Y);X = Y ++;// Post increment first assign than increment printf("X=%d Y=%d \n", X, Y); return 0;}
  • #15 XOR 1 1 = 0 ,0 1= 1, 0 0 =0 1 0=1