CP 1141- Introduction to Programming
Module -II
Chapter: 6
Operators and Expressions
• Operator is a symbol that tells the computer to perform some mathematical or
logical manipulations.
• Operators manipulates the data and variables.
• The operators of C are classified into different categories:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
Arithmetic Operators:
Arithmetic operators are used for numeric calculations. The different operators are
as follows:
Operator Meaning
+ Addition or unary plus A + B, +a
- Subtraction or unary minus A – B, -a
* Multiplication A * B, Product
/ Division A / B gives the quotient
% Modulo division A % B, gives the remainder
The different arithmetic operators are :
1. Unary arithmetic operator:- It requires only one operand. It indicates the sign of
the number.
Ex: +x, -y
2. Binary arithmetic operator:- It requires two operands.
• In A + B, A and B are the 2 operands.
• There are 5 operators for binary arithmetic (above given table)
3. Integer arithmetic: - When both the operands are integers, then this arithmetic
operation is called integer arithmetic.
Eg: A=10, B=3
Expression Result
A+ B 13
A- B 7
A*B 30
A/ B 3
A% B 1
4. Floating point arithmetic:- When both the operands are of float type, then the
arithmetic operation is termed floating point arithmetic.
Eg: A=10.1, B=3.2
Expression Result
A+ B 13.3
A- B 6.9
A*B 32.32
A/ B 3.15
A% B 5
5. Mixed mode arithmetic:- When one operand is real and the another is integer, the
expression is called mixed mode arithmetic expression.
• If any one of the operand is real, then the real operation is performed and the
result is always a real number.
Relational Operators:
• To compare two values based on their relation, we use the relational operator.
• An expression that contains the relational operator is called relational
expression.
• If the relation is true, it returns the value 1.
• If the relation is false, it returns the value 0.
• Relational operator compares the values to see if they are equal or if one of
them is greater than the other.
• Ex: 5<10, is true = 1.
15<10, is false = 0.
• C supports 6 Relational operators.
Operator Meaning
< Is less than
<= Is less than or equal to
> greater than
>= Greater than or equal to
== Is equal to
!= Is not equal to
Logical Operator or Boolean Operator:
• An expression that combines two or more expressions is termed as logical
expression. To combine the expressions we use the logical operator.
• The logical expression produces the result 1 (True) or 0 (False).
• The operands may be variables, constants and expression.
• There are 3 logical operators.
Operator Meaning
&& AND
|| OR
! NOT
AND Operator (&&):
This gives the result TRUE (1) only if the condition is TRUE, otherwise the result is
FALSE (0)
Condition 1 Condition 2 Result
True(1) True (1) True(1)
True(1) False(0) False(0)
False(0) True(1) False(0)
False(0) False(0) False(0)
Condition 1 Condition 2 Result
1 1 1
1 0 0
0 1 0
0 0 0
OR Operator (||):
This gives the result TRUE if any one of the value is TRUE, otherwise the result is
FALSE
Condition 1 Condition 2 Result
True True True
True False True
False True True
False False False
Condition 1 Condition 2 Result
1 1 1
1 0 1
0 1 1
0 0 0
NOT Operator(!):
• This is a unary operator.
• It negates the value of the condition.
• If the value of the condition is FALSE, then it gives the result TRUE.
• If the value of the condition is TRUE, then it gives the result FALSE.
Condition Result
False True
True False
Condition Result
0 1
1 0
Assignment Operator:
• To store the value in a variable, we use the assignment operator.
• This operator is also used as updation statement.
• Eg: x= 5;
a =a + 5; => a + = 5;
• Syntax: variable operator = expression;
Assignment Operator By using actual
assignment operator
A = A +1 A + =1
A = A -1 A-=1
A = A * ( n + 1) A * =(n + 1)
A=A / (n+1) A / = (n + 1)
A=A % B A%=B
The use of assignment operator are
• What appears on the left hand side need not be repeated and hence easy to
write.
• The statement is concise and easy to read.
• The statement is more efficient.
Increment and Decrement Operator:
Increment and decrement are Unary operators, they operate on single operand.
The increment operator (++)increments the value of the variable by 1.
The decrement operator (--)decrements the value of the variable by 1.
++a or a = a + 1
- -a or a = a – 1
These operators are of 2 types:
1. Prefix increment / decrement
2. postfix increment / decrement
Prefix increment / decrement:
First the value of the variable is incremented / decremented, then the value of variable
is taken for operation.
Example:
A=2;
B = ++A, here A is incremented, now A = 3, B = 3.
A=4;
B = --A, here A is decremented, A = 3, B = 3.
Postfix increment / decrement:
First the value of the variable is taken for operation then the value is incremented /
decremented.
Example:
A=2;
B = A++, here the value of A is assigned to B.
Now B=2, A=3.
Program for Increment and Decrement Operations
#include<stdio.h>
main()
{
int a = 10, b;
printf(“the value of a is %d”, a); ` a=5
printf(“the value of b is %d”, b = ++a); a=6 ,b=6
printf(“the value of b is %d”, b = --a); a= 5,b=5
printf(“the value of b is %d”, b = a++); b= 5, a=6
printf(“the value of b is %d”, b = a--); b= 6,a= 5
printf(“the value of a is %d”, a); a=5
}
Conditional operator:
• To express the conditional expressions an operator called conditional operator
or Ternary operator is used.
• A ternary operator uses 3 operands.
• The general form(syntax) of conditional operator is
• exp1 ? exp2 : exp3
• The operator ? performs the following.
• exp1 is evaluated. If it is a non-zero(True) then the exp2 is evaluated and
becomes the value of the expression.
• If exp1 is false then exp3 is evaluated and its value becomes the values of the
expression.
• i.e., only exp2 or exp3 is evaluated.
Eg: a=10, b=15
X = (a > b) ? a : b;
WAP to print logic 1 if the input character is Capital else print 0.
#include<stdio.h>
main()
{
char x;
printf(“Enter any single character”);
scanf(“ %c”, &x)
(x >= 65 && x <= 90) ? printf(“the result is logic 1”) : printf(“the result is logic 0”);
}
WAP to print logic 1 if the input character is Capital else print 0.
#include<stdio.h>
main()
{
char x;
int result;
printf(“Enter any single character”);
scanf(“ %c”, &x);
result = (x >= 65 && x <= 90) ? 1 : 0 ;
printf(“the result is %d “, result);
}
WAP to print logic 1 if the input is between 1 and 100 otherwise print 0.
#include<stdio.h>
main()
{
int x, result;
printf(“Enter any number”);
scanf(“ %d”, &x);
result = (x >= 1 && x <= 100) ? 1 : 0 ;
printf(“the result is %d “, result);
}
WAP to print logic 1 if the input is 1 or 100 otherwise print 0.
#include<stdio.h>
main()
{
int x, result;
printf(“Enter any number”);
scanf(“ %d”, &x);
result = (x == 1 || x == 100) ? 1 : 0 ;
printf(“the result is %d “, result);
}
WAP to find the largest of 2 numbers using conditional operator.
#include<stdio.h>
main()
{
int a, b, max;
printf(“enter the values for a and b”);
scanf(“%d %d”, &a, &b);
(a > b) ? printf(“A is greater”) : printf(“B is greater”);
}
Bitwise Operators:
• C has the ability to manipulate the data at bit level, so the bitwise operators are
used.
• The bitwise operators are operated on only integers and not in float and
double.
Bitwise operators Meaning
& Bitwise AND
| Bitwise OR
~ One’s complement
<< Left shift
>> Right shift
^ Bitwise XOR
The bitwise operators are used for testing the bits or shifting them right or left.
OP1 OP2 OP1& OP2 OP1 | OP2 OP1 ^ OP2
1 1 1 1 0
1 0 0 1 1
0 1 0 1 1
0 0 0 0 0
WAP to shift the data right by 2.
int main()
{
int x,y;
printf("\n enter the character");
scanf("%d", &x);
y = (x>>=2);
printf("\n the right shifted data is %d",y);
return 0;
}
WAP to shift the data left by 3
int main()
{
int x,y;
printf("\n enter the character");
scanf("%d", &x);
y=(x<<=3);
printf("\n the left shifted data is %d",y);
return 0;
}
Special Operators:
• There are several special operators in C. They are comma operator, size of
operator, pointer operator (* and &) and member selection operator.
• Comma Operator: This allows different expressions to appear simultaneously.
• Comma operator is used to separate two or more expressions.
• The separated expressions are evaluated from left to right and the rightmost
expression is the value of the compound expression.
• Example: sum = (a=1, b=2, c=4, a + b + c)
• The comma operator helps to make the code more compact.
WAP to interchange the values using comma operator.
#include<stdio.h>
main()
{
int a = 2, b = 4, temp;
printf(“ \n the value of a = %d , b = %d “, a, b);
temp = a, a = b, b = temp;
printf(“ \n the value of a = %d , b = %d “, a, b);
}
• WAP to add the contents using comma operator.
#include<stdio.h>
main()
{
int a , b , c , sum;
sum = (a= 1, b = 2, c = 3, a + b + c);
printf(“ \n the value of sum = %d “, sum);
}
Sizeof Operator:
• Sizeof is an unary operator. This gives the size of its operand in bytes.
• The operand may be a variable, constant or any data type.
• This is mainly used in arrays and structures when its size are unknown to the
programmer.
• It allocates the memory dynamically.
WAP to display the size of a variable or data type.
#include<stdio.h>
main()
{
int a, size;
size = sizeof(int);
printf(“ \n the size of a is = %d “, size);
}
WAP to print logic 1 if the input is 100 otherwise print 0.
#include<stdio.h>
main()
{
int x, result;
printf(“Enter any number”);
scanf(“ %d”, &x);
result = (x == 100) ? 1 : 0 ;
printf(“the result is %d “, result);
}
WAP to print logic 1 if the input is between 0 and 9 otherwise print 0.
#include<stdio.h>
main()
{
int x, result;
printf(“Enter any number”);
scanf(“ %d”, &x);
result = (x >= 0 && x <= 9) ? 1 : 0 ;
printf(“the result is %d “, result);
}