Unit 4
Unit 4
Operators
An operator is a symbol that operates on single or multiple data items.
Used in program to perform certain mathematical or logical manipulations.
The data items that operator act upon are called operands.
E.g. In a simple expression 2+3, the symbol “+” is called an operator which operates on
two data items 2 and 3.
Expression
An expression is a combination of variables, constants and operators written according to
syntax of the language. E.g. 7+8, x+y*z, a>b
Operator classification
a. According to Number of Operands
Unary operators
The operators which require only one operand to operate are called unary operators.
E.g. ++ (increment), -- (decrement) + (unary plus) and - (unary minus) are unary
operators.
Binary Operators
The operators which require two operands to operate are called binary operators. E.g.:
+ (plus), - (minus), *(multiply), / (division), <(less than), > (greater than), etc. are
binary operators.
Ternary Operators
The operators which require three operands to operate are called ternary operators.
1. Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations. There are five arithmetic
operators in C which are listed in table:
Division Rule:
int/int = int
float/float = float
int/float = float
float/int = float
Note: For modulo operator, the sign of the result is always the sign of the first operand. E.g.
10%3=1, -10%3=-1, -10%-3=-1, 10%-3=1 2
#include <stdio.h>
int main()
{
int days, months, remainingDays;
printf("Enter the number of days: \n");
scanf("%d", &days);
return 0;
}
2. Relational Operators
Relational operators are used to compare two operands and taking decisions based on their relation.
Result of relational expression is either True (1) or False (0).
Relational operators are used in decision making and loops.
Relational operators are:
/* Program to compare two numbers whether they are equal or not in C */
#include<stdio.h>
int main()
{
int m=40, n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
3. Logical Operators
Logical operators are used to compare logical and relational expression.
The operands of logical operators must be either Boolean value (1 or 0) or expression that
produces Boolean value.
The output of these operators is always 0 (flase) or 1 (true).
The logical operators are: Truth table for logical operators:
/* C program to demonstrate working of logical operators */
#include <stdio.h>
int main() {
int a = 5, b = 10, c = 15;
return 0;
}
4. Assignment Operator
Assignment operators are used to assign the values of an expression to a variable.
The mostly used assignment operator is ‘=’.
C also supports shorthand assignment operators which simplify operation with assignment.
/* program to demonstrate working of Assignment operators */
#include<stdio.h>
int main()
{
int a = 10;
printf("Value of a is %d\n", a); //10
a += 10;
printf("Value of a is %d\n", a); //20
a -= 10;
printf("Value of a is %d\n", a); //10
a *= 10;
printf("Value of a is %d\n", a); //100
a /= 10;
printf("Value of a is %d\n", a); //10
return 0;
}
7. Bitwise Operator
Bitwise operators are used for manipulating data at bit level.
These operators are used for testing the bits or shifting them to the left or to the right.
Can be applied only to integer-type operands and not to float or double.
Three types of bitwise operators:
i. Bitwise logical operators
ii. Bitwise shift operators
iii. One’s compliment operators
E.g.
If a = 65, b=15
Equivalent binary values of 65 = 0100 0001; 15 = 0000 1111
#include<stdio.h>
void main()
{
unsigned int a=15, left, right;
left = a<<3;
right =a>>2;
printf("%d\n", left);
printf("%d\n",right);
}
8. Special Operators
Comma operator (,):
The comma operator can be used link related expressions together.
A comma-linked list of expression are evaluated from left-to-right and the value of the rightmost
expression is the value of the combined expressions.
E.g. X=(a=5, b=10, a+b);
The first assign the value 5 to a
Assign the value 10 to b
Assign sum(a+b) to X
Sizeof operator
It is used with an operand to return the number of bytes it occupies.
The operand may be constant, variable or a data type qualifier.
Relational Expressions: Comparing values using relational operators like <, >, <=, >=, ==, !=
(less than, greater than, less than or equal to, greater than or equal to, equal to, not equal to).
Example: int comparison = (5 > 3); (evaluates to 1 for true, 0 for false)
Logical Expressions: Using logical operators like &&, ||, ! (logical AND, logical OR, logical
NOT) to combine multiple conditions.
Example: int condition = (a > 5 && b < 10); (evaluates to 1 for true, 0 for false)
Ternary Expressions: Using the conditional operator ? : to make a decision based on a condition.
Expressions can be as simple as a single value or can involve complex combinations of operators
and operands. They are evaluated following the rules of precedence and associativity defined by
the C language, where certain operators have higher precedence than others, determining the order
of evaluation.
Pre-increment and post-increment
Here’s a comparison table to illustrate the differences between pre-increment (++var) and post-
increment (var++) operators:
The pre-decrement (--var) and post-decrement (var--) operators operate similarly to their
increment counterparts but with a decrement operation.
Just like with increments, the key difference lies in whether the decrement occurs before or after
the current value of the variable is used within an expression.