Module 2-Operators in c
Module 2-Operators in c
➢ Arithmetic operators
➢ Relational operators
➢ Equality operators
➢ Logical operators
➢ Unary operators
➢ Conditional operators
➢ Bitwise operators
➢ Assignment operators
➢ Comma operator
➢ sizeof operator
1. ARITHMETIC OPERATORS
Arithmetic operators are used to perform arithmetic operations. Arithmetic operators
are evaluated from left to right.
Modulus operator (%) returns the remainder of the division of two operands.
Example: 14%2=0
7%5=2
2. RELATIONAL OPERATORS
3. EQUALITY OPERATORS
A B A&&B A !A
0 0 0 0 0
0 1 0 1 0
1 0 0
1 1 1
Logical OR
A B A||B
0 0 0
0 1 1
1 0 1
1 1 1
5. UNARY OPERATORS
UNARY MINUS:
When a number is preceded by a minus sign, the unary minus negates it value. For
example , if a=10, then b=-(a) =>b=-10.
y=x++;
is equivalent to y=x;
x=x+1;
y=x++;
y=x;
6. CONDITIONAL OPERATOR
The conditional operator or the ternary operator (? :) is just like an if-else statement
that can be within expressions.
exp1 is evaluated first.If it is true then exp2 is evaluated else exp3 is evaluated.
large= a>b: a : b;
7. BITWISE OPERATORS:
Bitwise operators perform operations at the bit level. The bitwise operators expect
their operands to be integers.
The different bitwise operators are:
Bitwise AND, Bitwise OR, Bitwise XOR, Bitwise NOT, Shift operator.
Eg:
A=1010 1010 ; B=0101 0101
A&B=0000 0000
A|B = 1111 1111
~A= 0101 0101
A^B = 1111 1111
Shift operator
C has 2 shift operators: left shift (<<) and right shift (>>)operator.
The syntax is
Operand op num;
Left shift operator- The bits are shifted to the left. The most significant bit (MSB) is
lost and the least significant bit is set to 0.
Eg: A= 0001 1101
A<<1 gives 0011 1010
A<<2 gives 0111 0100
Right shift operator- The bits are shifted to the right. The most significant bit (MSB)
is set to 0 and the least significant bit is lost.
Eg: A= 0001 1101
A>>1 gives 0000 1110
A>>2 gives 0000 0111
8. ASSIGNMENT OPERATOR
So a = b = c = 10;
is evaluated as a = (b = ( c = 10 )));
Eg: x += y is equivalent to x = x + y;
The other shorthand assignment operators are +=, -=, *=, /= , %=, &=, ^= , <<=, >>=.
9. COMMA OPERATOR
The comma operator in C takes two operands. It works by evaluating the first, and
discarding its value, and then evaluates the second and returns the value as the result
of the expression. The comma separated expressions are evaluated in the left to right
sequence. The comma operator has the least precedence.
Eg:
Int a=2, b = 3, x = 0;
x = (++a, b += a);
The sizeof operator returns the size of the variable, data type or expression in bytes. It
is used to determine the amount of memory space that the variable/ data type or
expression will take.
When an expression has more than one operator , then the relative properties of the operators
with respect to each other that determine the order in which the expression will be evaluated
is called precedence.
Associativity defines the direction in which the operator having the same precedence acts on
the operands. It can be either left – to- right or right- to – left.
Type conversion or typecasting of variables refers to changing a variable of one data type
into another.
Type Conversion:
Type conversion in C is the process of converting one data type to another. The type
conversion is only performed to those data types where conversion is possible. Type
conversion is performed by a compiler. In type conversion, the destination data type can’t be
smaller than the source data type. Type conversion is done at compile time and it is also
called widening conversion because the destination data type can’t be smaller than the source
data type.
Eg: float x;
int y = 3;
x = y;
Now x = 3.0, as the integer value is automatically converted to its equivalent floating point
representation.
Type casting
It is also called forced conversion. it is user-defined. Here the user can typecast the result to
make it of a particular data type.
(type) expression
type indicates the data type to which the final result is converted.
Eg: float salary = 10000.00;
int sal;
When a floating point value is assigned to an integer value, the digits after decimal are
truncated. Hence, some data might be lost.