Pop Module-2 Operators
Pop Module-2 Operators
Operand?
● Arithmetic operators
● Relational Operators
● Equality Operators
● Logical Operators
● Unary Operators
● Conditional Operators
● Bitwise Operators
● Assignment operators
● Comma Operator
● Sizeof Operator
Arithmetic operators:
This operator used for numeric calculation. These are of either Unary arithmetic
operator, Binary arithmetic operator. Where Unary arithmetic operator required only
one operand such as +,-, ++, --,!, tiled. And these operators are addition, subtraction,
multiplication, division. Binary arithmetic operator on other hand required two
Page 1
Module-2: Operators, Type conversion and Type casting
Note: Modulus operator is used to find the reminder of the integer division. This cannot
be used on float and double operands.
#include <stdio.h>
void main()
float a=5.0,b=2.0,A,S,M,D;
int ID,R;
A=a+b;//A=7.0
S=a-b;//S=3.0
M=a*b;//M=10.0
D=a/b;//D=2.5
printf("\n%f\n%f\n%f\n%f",A,S,M,D);
Page 2
Module-2: Operators, Type conversion and Type casting
int c,d;
c=a;
d=b;
ID=c/d;//ID=2
R=c%d;//R=1
printf("\n%d\n%d",ID,R);
}
Relational operators:
• Also known as a comparison operator, it is an operator that compares two values.
• Relational operators return true or false value, depending on whether the conditional
relationship between the two operands holds or not.
Equality operators
• C language supports two kinds of equality operators to compare their operands for
strict equality or inequality. They are equal to (==) and not equal to (!=) operator.
• The equality operators have lower precedence than the relational operators.
Page 3
Module-2: Operators, Type conversion and Type casting
void main()
int a=10,b=20;
printf("\n %d == %d = %d",a,b,a==b);//0
printf("\n %d != %d = %d",a,b,a!=b);//1
Logical operators
• Operator used with one or more operand and return either value zero (for false) or
one (for true). The operand may be constant, variables or expressions. And the
expression that combines two or more expressions is termed as logical expression.
• C language supports three logical operators. They are- Logical AND (&&), Logical OR
(||) and Logical NOT (!).
• As in case of arithmetic expressions, the logical expressions are evaluated from left to
right.
Page 4
Module-2: Operators, Type conversion and Type casting
Example:
#include <stdio.h>
void main()
int a, b;
void main()
{
int a=5,b=5,c=10,R;
R=(a==b)&&(c>b);
printf("(a==b)&&(c>b) is %d\n",R);//1
R=(a==b)&&(c<b);
printf("(a==b)&&(c<b) is %d\n",R);//0
R=(a==b)||(c<b);
printf("(a==b)||(c<b) is %d\n",R);//1
R=!(a!=b);
printf("!(a!=b) is %d\n",R);//1
R=!(a==b);
printf("!(a==b) is %d\n",R);//0
}
Unary operator
• C language supports three unary operators. They are unary minus(-), increment(++)
and decrement operators(--).
• The increment operator is a unary operator that increases the value of its operand by
1.
Unary minus:
• When an operand is preceded by a minus sign, the unary operator negates its value.
Example:
#include <stdio.h>
void main()
int a,b=10;
a=-(b);
printf("%d",a);//-10
Example:
y=x++
Page 6
Module-2: Operators, Type conversion and Type casting
y=x;
x=x+1;
y=x;
x=x-1;
Example:
y=++x
x=x+1;
y=x;
x=x-1;
y=x;
Example:
#include <stdio.h>
void main()
int a=10;
printf("++a=%d\n",++a);//1
printf("--a=%d\n",--a);//10
printf("a++=%d\n",a++);//10
printf("a--=%d\n",a--);//11
Page 7
Module-2: Operators, Type conversion and Type casting
Conditional operator:
• The conditional operator operator (?:) is just like an if .. else statement that can be
written within expressions.
Here, exp1 is evaluated first. If it is true then exp2 is evaluated and becomes
the result of the expression, otherwise exp3 is evaluated and becomes the result
of the expression.
#include <stdio.h>
void main()
int age=17;
void main()
{
int a=10,b=5,c=16,large;
large=(a>b)?a:b;
printf(“%d”,large);
#include <stdio.h>
void main()
int a=10,b=5,c=16,large;
Page 8
Module-2: Operators, Type conversion and Type casting
large=(a>b)?(a>c?a:c):(b>c?b:c);
printf(“%d”,large);
Bitwise operator
• Bitwise operators perform operations at bit level. These operators include: bitwise
AND, bitwise OR, bitwise XOR and shift operators.
• The bitwise AND operator (&) is a small version of the boolean AND (&&) as it
performs operation on bits instead of bytes, chars, integers, etc. Bit in the first operand
is ANDed with corresponding bit in the second operand.
• The bitwise NOT (~), or complement, is a unary operation that performs logical
negation on each bit of the operand. By performing negation of each bit, it actually
produces the ones' complement of the given binary value.
• The bitwise XOR operator (^) performs operation on individual bits of the
operands. The result of XOR operation is shown in the table
• Bitwise left shift(<<): When we apply left shift, every bit in the operand is shifted to
left by one place.
if x= 0001 1101
• Bitwise right shift(>>): When we apply right shift, every bit in the operand is
shifted to right by one place.
Page 9
Module-2: Operators, Type conversion and Type casting
if x= 0001 1101
#include<stdio.h>
void main()
{
int a=27,b=39;
printf(“a&b=%d\n”,a&b);//3
printf(“a|b=%d\n”,a|b);//63
printf(“~a=%d\n”,~a);//-28
printf(“a^b=%d\n”,a^b);//60
printf(“a<<1=%d\n”,a<<1);//54
printf(“b>>1=%d\n”,b>>1);//19
}
Assignment operator
• While the equal sign (=) is the fundamental assignment operator, C also supports
other assignment operators that provide shorthand ways to represent common variable
assignments. They are shown in the table.
Page 10
Module-2: Operators, Type conversion and Type casting
#include <stdio.h>
void main()
{ int a=5,c=10;
printf("c/=a=%d\n",c/=a); //c=c/a,c=2
printf("c*=a=%d\n",c*=a); //c=c*a,c=10
printf("c-=a=%d\n",c-=a); //c=c-a,c=5
printf("c+=a=%d\n",c+=a); //c=c+a,c=10
printf("c&=a=%d\n",c&=a); //c=c&a,c=0
printf("c^=a=%d\n",c^=a); //c=c^a,c=5
printf("c<<=a=%d\n",c<<=a); //c=c<<a,c=160
printf("c>>=a=%d\n",c>>=a); //c=c>>a,c=5}
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.
• Among all the operators, the comma operator has the lowest precedence. For
example,
x = (++a, b+=a);
++a=> a=a+1
a=3
b+=a=> b=b+a
b=3+3=6
Size of operator
Page 11
Module-2: Operators, Type conversion and Type casting
• The operator returns the size of the variable, data type or expression in bytes.
• 'sizeof' operator is used to determine the amount of memory space that the
variable/expression/data type will take.
#include <stdio.h>
void main()
int a;
float b;
double c;
char d;
printf("size=%u\n",sizeof(a));
printf("size=%u\n",sizeof(b));
printf("size=%u\n",sizeof(c));
printf("size=%u\n",sizeof(d));}
Type Conversion
• Type conversion and type casting of variables refers to changing a variable of one
data type into another.
• While type conversion is done implicitly, casting has to be done explicitly by the
programmer. We will discuss both of them here.
• Type conversion is done when the expression has variables of different data types.
So to evaluate the expression, the data type is promoted from lower to higher level
where the hierarchy of data types can be given as: double, float, long, int, short and
char.
For ex,
float x;
int y = 3;
Page 12
Module-2: Operators, Type conversion and Type casting
x = y;
Now, x = 3.0,
Type Casting
• Type casting is also known as forced conversion. It is done when the value of a
higher data type has to be converted in to the value of a lower data type. For example,
we need to explicitly type cast an integer variable into a floating point variable.
float salary = 10000.00;
int sal;
Page 13