Chapter 4
Operators in C
What is an operator?
Operator is a symbol which operates on an Operand .
It tells the compiler to perform specific operations on
its operand.
Operators are used to manipulate data in other words
operands.
Operators can be classified in two ways
Based on number of operands
Based on the role of operators
Based on Number of operands
Unary operator:
can be applied on only one operand
Ex: ~ ! sizeof ++ --
Binary operator:
can be applied on two operands
Ex: + *
Ternary operator:
can be applied on three operands
Ex: ? : (Conditional or ternary operator)
Based on the Role of Operators
Arithmetic operators
Bitwise operators
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators
Special operators
Arithmetic Operators
Operators used to perform arithmetic operations.
Operator Function Example
+ Addition 8+4=12
_ Subtraction 8-4=4
* Multiplication 8*4=32
/ Division 8/4=2
% Modulo(Remainder of 8%4=0
Division)
+ and – operators also behave as unary operators.
Contd..
% cannot be used on floating point numbers.
C does not have an exponentiation operator.
During integer division result is truncated towards zero
if both operands are of same sign.
EX: 8/9=0, -8/-9=0, -8/9= 0 or -1 (machine dependent).
During modulo division the sign of the result is always
the sign of the first operand.
Ex: 14% -3=2 -14%3= -2
Bitwise Operators
Operators which operates on bit level.
It works only on int data type variables.
Operators Meaning Example
& Bitwise AND 6&7 = 6
| Bitwise OR 6|7 = 7
~ Bitwise Complement ~6 = 1
or Negation
<< Left Shift 6<<2 = 24
>> Right Shift 6>>2 = 1
^ Bitwise XOR 6^7 = 1
Truth Table of Bitwise operators
True is any value other than zero. False is zero.
Bitwise Operators Contd..
~ operator gives one’s complement of operand.
Bit Shift operators >> and << moves the bits by the
number of position as specified .
Bitwise shift operators is not a rotate operation.
Left shift is equivalent to multiplication by 2n.
Right shift is equivalent to division by x/2n
Where n is the number of bits to be shifted.
Only Bitwise complement is a unary operator, with the rest
being binary operators.
Relational Operators
Relational operators checks relationship between two operands.
If the relation is true, it returns value one
If the relation is false, it returns value zero.
Operator Function
< Is less than
> Is greater than
<= Is less than or equal to
>= Is greater than or equal to
== Is equal to
!= Is not equal to
Logical Operators
Logical operators are used to combine expressions containing
relational operators .
If the relation is true, it returns value one.
if the relation is false, it returns value zero.
Operators Meaning
Logical And and
&& Logical AND Logical OR
consist of 2
|| Logical OR characters unlike
! Logical NOT bitwise operators
There are no logical XOR and XNOR
operators
Relational operators functioning
Consider an example where we want to check if the
marks of a student is greater than 80.
Suppose marks scored Suppose marks scored
by the student is 75 by the student is 80
Now using relational operators Marks > 80 gives 0
Marks > 80 gives 0 Marks >= 80 gives 0 Marks < 80 gives 0
Marks >= 80 gives 1
Marks < 80 gives 1 Marks < = 80 gives 1 Marks <= 80 gives 1
Marks == 80 gives 1
Marks == 80 gives 0 Marks != 80 gives 1
Marks != 80 gives 0
Logical operators functioning
Now if we want to see if the Marks of a student is greater than 80 and
also if the Subject is english
Suppose marks scored by the student is 75 and the subject is english
Now using relational and logical operators
(Marks > 80)&&(Subject == English) gives 0 Truth table of
0 && 1 logical operators
are similar to
(Marks < 80)&&(Subject == English) gives 1 that of respective
1 && 1 bitwise operators
(Marks > 80)||(Subject == English) gives 1
0 || 1
(Marks < 80)||(Subject == English) gives 1
1 || 1
Cont…
The Logical ! Operator should be used carefully.
For E.g the result of the expression
(Marks < 80)&&(Subject == English) gives 1
1 && 1
Can be complemented by the expression
!((Marks < 80)&&(Subject == English)) gives 0
! (1 && 1)
logical and relational operators Also see the
are used a lot together with the difference between
relational expressions enjoining the ! and | symbols
the logical operators
Assignment Operators
Operators are used to assign the result of an expression to a
variable.
variable operand = expression
C also provides “short-hand-assignment” operators which are
given as below.
spot the
difference
between
relational
operator == and
assignment
operator =
Increment and Decrement Operators
These are Unary Operators
They can be applied only to variables
They cannot be applied on constants or ordinary expressions.
C supports 2 useful operators namely
1. Increment ++
2. Decrement – -
These operators can either prefix or postfix the operand.
Prefix ++x; or Postfix x++;
--x; or x--;
They can be used only on variables.
Consider int R,count=10;
Conditional Operator
Also known as Ternary operator.
A powerful and convenient operator that replaces certain statements of
the if-then-else form.
It uses the ? And : symbols in between the 3 operands.
Exp1 ? Exp2: Exp3
Here Exp1 consists of
relational and logical
Stands for: if Exp1 operators
then Exp2 Nesting of conditional
else Exp3 operator is possible
in C
E.g int num1=10, num2=15, big ;
big = (num1>num2) ? num1 : num2 ;
For the given example the variable big will be assigned the value of num2.
Comma Operator
Used to combine two or more C statements into a single C
statement.
It is a binary operator.
expression_1, expression_2 ;
Comma Operators can be useful in control statements though
C programmers discourage their use.
Ex: temp=a, a=b, b=temp;
instead of temp=a;
a=b ;
b= temp;
sizeof Operator
sizeof() operator returns number of bytes the operand
occupies.
Syntax: sizeof(operand);
Ex: main() operand can be a
{ constant, data type
or a variable.
int a;
printf(“%d”, sizeof(a));
}
// It returns 2 bytes (machine dependent)
Program to find Maximum
of 03 integers using
Conditional operator
Maximum of 03 integers using Conditional operator
/* Program to find Maximum of 03 integers using
Conditional operator */
#include <stdio.h>
main()
{ int a,b,c;
printf("enter 3 integers :");
scanf("%d%d%d",&a,&b,&c);
printf("Maximum Value of %d %d %d is %d",
a,b,c,a>b ? (a>c ? a : c) :( b > c ? b : c));
}
Precedence of Operators
Operator precedence describes the order in which C reads
expressions.
For E.g the expression
a=4+b*2
three operators, an addition , a multiplication and an
assignment operator.
Does the C compiler evaluate 4+b first, then multiply
the result by 2,
Or does it evaluate b*2 first, then add 4 to the result?
C compiler evaluates operators having a higher precedence first.
As a result the second option is carried out.
Operator Precedence Chart
OPERATOR TYPE OPERATORS ASSOCIATIVITY
Unary ! + - ++ -- Right to left
Multiplicative * / % Left to right
Additive + -
Relational < > <= >=
Equality == !=
Logical And &&
Logical OR ||
Conditional expression ?: Right to left
Assignment = += -= *= /= %=
Comma , Left to right
Associativity of Operators
When we have operators on the same precedence level, then the
"Associativity" property gives their evaluation order.
Left to right
Right to left
For E.g. the expression a=4+b-2
three operators, an addition , a subtraction and an
assignment operator.
Here the C compiler evaluate 4+b first, then subtracts the
result by 2. (since the associativity is left to right)
The associativity of unary Evaluation of expressions
operators and assignment enclosed within () supersedes
operators is right to left all the operator precedence.
Type Conversions
When variables and constants of different data types are combined
in an expression then they are converted to same data type.
The process of converting one predefined type into another is
called type conversion.
There are 2 types of type conversions
Implicit type conversion
Explicit type conversion
Implicit Type Conversion
Here the type conversion is performed automatically by the compiler
without the programmers intervention.
The compiler converts all operands into the data type of the largest
operand.
It should be noted that the final result of expression is converted to
type of variable on left side of assignment operator before assigning
value to it.
Implicit Type cast chart
Explicit Type Conversion
Here the type conversion is performed by the programmer.
Type casting in c is done in the following form:
(data_type)expression;
data_type is any valid c data type
expression may be constant, variable or expression.
E.g. : Consider 2 variables male_count and female_count
which are of int data type and we are required to find the
ratio which is of float data type.
now if we have the expression
ratio=male_count/female_count;
We do not get the desired result since both the variables on the
right are of type int and hence there is loss of data when assigned
to a variable of type float on the left of the assignment operator.
Explicit Type Conversion Cont …
Now the programmer can explicitly typecast one of the int
variables.
ratio = (float)male_count/female_count;
Here the variable male_count is treated as float in the
expression, but is not treated as float variable elsewhere.
Some of the operators have dual behavior, which depends upon
the statement context in which they are employed
For E.g. & Behaves as Bitwise And operator.
Behaves as address operator in scanf().
* Behaves as Arithmetic Multiplication operator.
Behaves as pointer declaration operator with variables.
If the parentheses are nested, evaluation of expressions begins
with innermost sub expression which have higher priority.
In case of relational operators it is an error if there is space
between the two symbols.
!= ! =
x=a>x ? b : c>y? d : e;
is interpreted as:
x = a > x ? b : (c > y ? d : e) ;
OR
x = (a > x ? b : c > y ) ? d : e ; ???
What are operators and operands in an expression ?
Mention the various types of operators in C with
examples.
Explain the following with an example
Precedence of an operator
Associativity of an operator
Mention the different type conversions in C with
relevant examples.
Write short notes on the following operators in C with
relevant examples
Relational Logical Bitwise
Conditional Assignment Special