You are on page 1of 25

UNIT-2

OPERATORS AND CONTROL STATEMENTS

OPERATORS:
An Operator is a symbol that tells the compiler to perform certain mathematical or
logical manipulations. C language is rich in built-in operators.

C Operators can be classified into a number of categories. They include:

1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Equality operator
6. Increment and Decrement operators
7. Conditional operators
8. Bitwise operators
9. Special operators

1. Arithmetic operators: C provides all the basic arithmetic operators. The operator +, -, *, %.

Operator Meaning

+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Division
Integer division truncates any fraction part. The modulo division produces the remainder of an
integer division.

Example of Arithmetic Operators

a-b, a+b, a*b, a/b, a%b.

Here a&b are variables and are known as operands. The modulo division operator % cannot be
used on floating point data.

In this Arithmetic operator three types are there.

a. Integer Arithmetic: When both the operands in a single arithmetic expression, the expression
is called an integer expression, and the operation is called integer arithmetic.
If a&b are integers, then for a=14 and b=4

a-b=10
a+b=18
a*b=56
a/b=3 (decimal part truncated)
a%b=2 (remainder)

During integer division, if both the operands are of the same sign, the result is truncated towards
zero. If one of them is negative, the direction of truncation is machine dependent.

6/7=0 -6/-7=0 -6/7 may be 0 or -1

During modulo division, the sign of the result is always the sign of the first operand.

-14%3=-2
-14%-3=-2
14%-3=2
14%3=2
b. Real Arithmetic: An Arithmetic operation involving only real operands is called real
arithmetic. A real operand may assume values either in decimal or exponential notation.

Ex:

x=6.0/7.0=0.857143
y=1.0/3.0=0.333333
z=-2.0/3.0=-0.666667
The operator % cannot be used with real operands.

c. Mixed-mode Arithmetic: When one of the operands is real and the other is integer ,the
expression is called a mixed-mode arithmetic expression .Then the result is always is always a
real number.

15/10.0=1.5
15+10.0=25.0

2. Relational operator: We often compare two quantities, and depending on their relation, take
certain decisions. For ex: we may compare the age of 2 persons or the price of 2 items, and so
on. These comparisons can be done with the help of relational operators.

Operator Meaning

< is less than


<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to
The value of a relational expression is either one or zero. It is one , if the specified relation is true
and zero if the relation is false.

True 1 10<20 is true


False 0 20<10 is false

Ex: Write a C program to use various relational operators & display the return values.

#include<stdio.h>
void main()
{
printf(“Condition : Return values \n”);
printf(“\n10<=10:%5d”,10<=10);
printf(“\n10>=100:%5d”,10>=100);
printf(“\n10==10:%5d”,10==10);
printf(“\n5!=6:%5d”,5!= 6);
}
Output:

Condition : Return values


10<=10 1
10>=100 0
10==10 1

3. Logical Operators:

The logical operators && and || are used when we want to test more than one condition. It
is used to combine two or more relational expressions. Logical Operators are used in decision
making. Logical expression yields value 0 or 1 i.e., (Zero or One). 0 indicates that the result of
logical expression is TRUE and 1 indicates that the result of logical expression is FALSE.
Operator Meaning

&& Logical AND


|| Logical OR
| Logical NOT
Ex: a>b && x==10

An expression of this kind which combines two or more relational expressions is termed as a
logical expression.

Truth Table:

OP1 OP2 OP1 && OP2 OP1 || OP2


1 1 1 1
1 0 0 1
0 1 0 1
0 0 0 0

The logical expression yields value of one or zero according to the truth table.

Ex:

age>55 && salary<1000

number<0 || number>100

Ex: Write a C program to illustrate the use of logical operators

#include<stdio.h>
void main()

printf(“\n condition : return values\n”);

printf(“\n5>3 && 5<10:%5d”,5>3 && 5<10);

printf(“\n8>5 || 8<2:%5d”,8>5|| 8<2);

printf(“\n!(8==8):%5d”,!(8=8));

Output:

Condition : Return values


5>3 && 5<10 : 1
8>5 || 8<2 : 0
!(8==8) : 0

4. Assignment Operators: Assignment Operators are used to assign the result of an expression
to a variable. We have seen the usual assignment operator ’=’.

C has set of ‘shorthand’ assignment operators of the form.

v op=exp;

Where v is a variable,
exp is an expression and
op is binary arithmetic operator.
The operator op= is known as the shorthand assignment operator.

Ex:

x+=y+1;

is same as x=x+(y+1);

statement with simple statement with


assignment operator shorthand operator

a+=1
a=a+1
a-=1
a=a-1

a=a*(n+1) a*=n+1
a/=n+1
a=a/(n+1)
a%=b
a=a%b
5. Increment and Decrement
Operators (or) Unary Operator: C has two very useful operators not generally found in other
languages. These are the increment and decrement operators.
++ and - -
The operator ++ add 1 to the operand.
The operator - - subtracts 1 to the operand.
Increment and decrement operators are divided into
post increment post decrement
pre increment pre decrement

Post Increment: A Postfix operator first assigns the value to the variable on left then increments
the operand.
a++
a++ is equivalent to a=a+1 or a+=1
Ex:
a=10
a=10
a++
a=11

Ex: Write a C program to illustrate the post increment.


#include<stdio.h>
void main()
{
int x=10,y=20,z,a;
z=x*y++;
a=x*y;
printf(“%d%d”,z,a);
}

Output: 200
210

Pre Increment: A Prefix operator first adds 1 to the operand and then the result is assigned to
the variable on left.
++a
Ex: a=10
a=11
++a
a=11

Ex: Write a C Program to illustrate the pre increment.


#include<stdio.h>
void main()
{
int x=10,y=20,z,a;
z=x*++y;
a=x*y;
printf(“%d%d”,z,a);

}
Output: 210
210

Post Decrement: A Postfix operator first assigns the value to the variable on left then
decrements the operand.
a--
a-- is equivalent to a=a-1 or a-=1

Ex: a=10
a=10
a--
a=9
Ex: Write a C program to illustrate the Post decrement
#include<stdio.h>
void main()
{
int x=10,y=20,z,a;
z=x*y--;
a=x*y;
printf(“%d%d”,z,a);

}
Output: 200
190

Pre decrement: A Prefix operator first subtracts 1 to the operand and then the result is assigned
to the variable on left.
--a
Ex: a=10
a=9
--a
a=9

Ex: Write a C program to illustrate the Pre decrement.


#include<stdio.h>
void main()
{
int x=10,y=20,z,a;
z=x*--y;
a=x*y;
printf(“%d%d”,z,a);
}

Output: 190
190

6. Conditional Operators (or) Ternary Operator: A Ternary operator pair “? :” is available in


C.
exp1? exp2:exp3;
Where exp1, exp2&exp3 are expressions
(or)
condition? exp1:exp2;
If Condition must be written before ‘?’
The two expression are separated by ’:’.
where exp1 is the condition which is to be checked and exp2 is the true value and exp3 is the
false value. If the condition in exp1 is true then statement in exp2 is executed and if the condition
in exp1 is false then statement in exp3 will be automatically executed.

Ex:
(3>2)?3:2;

True
Output: 3

Ex 1: Write a C program to use conditional operand with two values


#include<stdio.h>
void main()
{
printf(“Result%d”,2==3?3:4);
}
Output: Result 4

Ex 2: Write a C program to use conditional operator with 2 statements.


#include<stdio.h>
void main()
{

56>6? printf(“True”):printf(“False”);
}
Output: True

Ex 3: Determine the value of b using conditional statement?


#include<stdio.h>
void main()
{
int b;
b=(5>6?6:7);
printf(“Result is %d”,b);
}
Output: Result is 7.

7. Bitwise Operator:

 Bitwise operators for manipulation of data at bit level. These operators are used for
testing the bits (or) shifting them right or left.
 Bitwise operators may not be applied for float or double.
 Bitwise operators only operate on integer operands. Such as int, short, long int, char.

Operator Meaning

& Bitwise AND

| Bitwise OR Bitwise Logical operators

^ Bitwise exclusive OR

>> Right shift

<< Left shift Bitwise shift operators

~ One’s complement

Bitwise Logical operators:

Bitwise AND: (&)


It represent by single ampersand (&).

Tabular representation:

Op1 Op2 Op1&op2

1 1 1

1 0 0

0 1 0

0 0 0

Syntax: op1&op2

Ex:- Let a=8


15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0

b=4

15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

C=0

Some Ex:-

8&8=8

13&25=9

2&3=2

EX:- Write a C program to use Bitwise AND

#include<stdio.h>

void main()
{

int a,b,c;

printf(“Enter a,b”);

scanf(“%d%d”,&a,&b);

c=a&b;

printf(“Bitwise ANDing is=%d”,c);

Output: Enter a,b 8 4

Bitwise ANDing is= 0

Bitwise OR: (|)

It represent by vertical bar (|)

Tabular representation:

Op1 Op2 Op1|op2

1 1 1

1 0 1

0 1 1

0 0 0

Syntax: op1|op2

Ex:- Let a=8

15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
b=4

15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

C=12

Some Ex:-

8|8=8

13|25=29

2|3=3

EX:- Write a C program to use Bitwise OR

#include<stdio.h>

void main()

int a,b,c;

printf(“Enter a,b”);

scanf(“%d%d”,&a,&b);

c=a|b;

printf(“Bitwise ORing is=%d”,c);

Output: Enter a,b 8 4

Bitwise ORing is= 12

Bitwise Exclusive OR: (^)


It represent by the symbol (^).

Tabular representation:

Op1 Op2 Op1^op2


(op1’op2+op1op2’)

1 1 0

1 0 1

0 1 1

0 0 0

Syntax: op1^op2

Ex:- Let a=8

15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0

b=2
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0

Some Ex:-

8^2=10

13^25=20

2^3=?

EX:- Write a C program for Bitwise Exclusive OR operation between two integers.
#include<stdio.h>

void main()

int a,b,c;

printf(“Enter a,b”);

scanf(“%d%d”,&a,&b);

c=a^b;

printf(“Bitwise Exclusive ORing is=%d”,c);

Output: Enter a,b 8 2

Bitwise Exclusive ORing is= 10

Bitwise Shift operators:

Left shift:

The shift operators are used to move bit patterns either to the left or the right. The shift operators
are represented by the symbols << and >>.

Left shift: op<<s

op is the integer expression that is to be shifted.

s is the number of bit positions to be shifted.

1. The left shift operation causes all the bits in the operand op to be shifted to the left by s
positions.
2. The left most bits in the original bit pattern will be lost and the right most s bit positions
that are vacated will be filled with 0’s.

Ex:- n<<3

Let n=2
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0

15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0

15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0

Note: Shifting s bits left means number (n) is multiplied by 2s

Y=n*2s

Where n=number

s=no. of positions to be shifted

Y=2*23

= 16

Ex: Write a C program to use left shift operator.

#include<stdio.h>

void main()
{

int x,y;

printf(“read x”);

scanf(“%d”,&x);

x<<3;

y=x;

printf(“Left shifted data is=%d”,y);

Output: read x 2

Left shifted data is =16

Right shift:

op to be shifted to the right by s positions.

Left shift: op>>s

op is the integer expression that is to be shifted.

s is the number of bit positions to be shifted.

1. The right most s bits will be lost. The left most s bit positions that are vacated will be
filled with zero.
2. If op is an unsigned integer. If the variable to be shifted is signed, then the operation is
machine dependent.
3. op and s are constants or variables. Two restrictions on value of s.
a. It may not be –ve.
b. It may not exceed the number of bits used to represent the left operand op.

Ex:- n>>2

Let n=8
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0

Note: Shifting s bits right means number (n) is divided by 2s

Y=n/2s

Where n=number

S=no. of positions to be shifted

Y=8/22

=2

Ex: Write a C program to use right shift operator.

#include<stdio.h>

void main()

int x,y;

printf(“read x”);

scanf(“%d”,&x);

x>>2;

y=x;

printf(“Right shifted data is=%d”,y);


}

Output: read x 8

Right shifted data is =2

Bitwise complement operator: The complement operator (~) is a unary operator. Also called
one’s complement operator.

Op Op1

0 1

1 0
Ex:- x= 1001 0110 1100 1011

~x= 0110 1001 0011 0100

Ex: Write a C program to use bitwise complement operator.

#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int a=10; int a=10;
a=~a; a=~a;
printf(“a=%d”,a); printf(“a=%u”,a);
} }
Output: a=-11 Output: a=65525

Special Operators: C Supports some special operators such as comma operator, sizeof
operator, pointer operators (& and *) and member selection operators (. and ->).
Special operators are known as separators or punctuators. Special operators are

Ampersand ( & ) or Address of Operator


Asterisk ( * ) or Indirection Operator
Braces ( { } )
Colon ( : )
Brackets ( [] ) or Array Subscript Operator
Comma ( , )
Hash ( # )
Parenthesis ( () ) or Function call Operator
Semicolon ( ; )
sizeof()

Ampersand ( & )

It is also known as address operator. It is specified before the identifier name. i.e. variable name.
It indicates memory location of the identifier.

It is denoted by ‘&’.

When used as a prefix to a variable name ‘&’ operator gives the address of that variable.

Example: &n – It gives address on variable n.

#include<stdio.h>
void main()
{
int n=10;
printf(“\n value of n is : %d”,n);
printf(“\n value of &n is : %u”, &n);
}
Output
value of n is : 10
Value of &n is:1002
Asterisk ( * )

Asterisk ( * ) is also known as indirection operator. It is specified before identifier name.


It indicates creation of pointer variable. It is also a unary operator.

Braces ( { } )
The opening brace ( { ) and closing brace ( } ) specify the start and end of compound
statement in a function.

Brackets

Brackets [] also referred as array subscript operator. It is used to indicate single and multi
dimensional arrays.

Eg: int x[10]; float l[10][20];

Colon ( : )

Colon ( : ) is used in labels. It is used in unconditional control statement i.e., in goto


statement.

Comma Operator ( , )

It is used to link expressions together. It is used together with variables to separate one
variable from another. It is used in for loop. It has the lowest precedence among operators

Eg: for(n=1,m=10;n<=m; n++, m++)


int a,b,c;

sum= (x=5,y=3,x+y);

The comma operator can be used to link the related expressions together.
Comma-linked lists of expressions are evaluated left to right and the value of right-most
expression is the value of the combined expression.
Ex:
value=(x=10,y=5,x+y);
Hash ( # )

Hash ( # ) is also known as pound sign. It is used to indicate preprocessor directives.

Eg: #include<stdio.h>

Parenthesis ( () )
Parenthesis ( () ) is also known as function call operator. It is used to indicate the open
and end of function prototypes, function call, function parameters. Parentheses are used to group
expressions.

Semicolon ( ; )

Semicolon ( ; ) is a statement delimiter. It is used to end a C statement.

Eg: g=d+h;

Sizeof Operator: The sizeof is a compile time operator and, when used with an operand, it
returns the number of bytes the operand occupies. The operand may be a variable, a constant or a
data type.
Ex:
m=sizeof(int);
Ex: Write a C program to show the usage of sizeof operator.
#include<stdio.h>
void main()
{
int a=10;
float b=20.7;
printf(“%d%d”,sizeof(a),sizeof(b));
printf(“Address of a=%u,Address of b=%u”,&a,&b);
}
Output: 2
4
4096
5000
EXPRESSIONS:
An expression is a combination of variables, constants and operators arranged as per the
syntax of the language.

Evaluation of Expression:
Expressions are evaluated using.
variable =expression;

Algebraic Expression C Expression


a*b-c a*b-c
(m+n)(x+y) (m+n)*(x+y)
ab/c (a*b)/c
3x2 +2x+1 3*x*x+2*x+1
x/y+c x/y+c
Based on the number of operators present in the expression, expressions are classified into two types:
Simple Expression and Compound Expression.

Simple Expression: An expression that has only one operator is called Simple Expression.

Example: a=a+2;

Compound Expression: While an expression that involves more than one operator is called Compound
expression.

Example: b=2+3*5;

The evaluation of simple expression is easier then compound expression, because we have more
operators in compound expression, while evaluating compound expression we must determine the order
in which operators will operate.

For example, to determine the result of evaluation of the expression b=2+3*5, we must determine the
order in which =,+ and * or *,+ and = will operate in these cases the result may be different. To avoid this
situation the operators will operate depends upon the Precedence and the Associativity of the operators.

These expressions are to be evaluated with the help of operators priority. i.e; precedence of
operators & associativity.
Precedence of operators:

Each operator in C has precedence associated with it. In compound expression, if the operators involved
are of different precedence, the highest precedence operator will operate first.

For example, b=2+3*5

Associativity of Operators:

In a compound expression, when several operators of the same precedence appear together, the operators
are evaluated according to their Associativity. An operator can be either left – to – right associative or
right – left – associative.

The operators with the same precedence always have the same associativity. Let us look at various
operators, their classification, precedence and associativity.

Precedence of Arithmetic Operators:


An Arithmetic expression without parentheses will be evaluated from left to right using
the rules of precedence of operator.
High Priority * / %
Low Priority + -

Operator Operation Clubbing(Associativi Priority(Precedence)


ty)
( ) Function call
[ ] Array expression Left to right 1st
Structure operator

+ Unary plus
- Unary minus
++ Increment
-- Decrement
! Not operator

~ One’s complement
* Pointer operator Right to left 2nd
& Address operator
Sizeof Size of an object
Type Typecast

* Multiplication
/ Division Left to right 3rd
% Modular division

+ Addition Left to right 4th


- Subtraction

<< Left shift Left to right 5th


>> Right shift

< Less than


<= Less than or equal to Left to right 6th
> Greater than
>= Greater than or equal
to

== Equality Left to right 7th


!= Inequality

& Bitwise AND Left to right 8th

^ Bitwise XOR Left to right 9th


| Bitwise OR Left to right 10th

&& Logical AND Left to right 11th

|| Logical OR Left to right 12th

?: Conditional operator Right to left 13th

=,*=,-=,&= Assignment Right to left 14th


+=,^=,|=,<<=,>>= Operators

, Comma operator Left to right 15th

Ex:
x=a-b/3+c*2-1
t1

step1 - a-t1+c*2-1

t2
step2 - a-t1+t2-1

t3

step3 - t3+t2-1

t4
Step4- t4-1
Step 5- t5
t5
Ex: x=a-b/3+c*2-1
Here a=9, b=12, c=3 Assumed.

X=9-12/3+3*2-1
=9-4+3*2-1
=9-4+6-1
=5+6-1
=11-1
=10

Ex: Write a C program to evaluation of Expressions.


#include<stdio.h>
void main()
{
float a,b,c,x,y,z;
a=9;
b=12;
c=3;
x= a-b/3+c*2-1;
y= a-b/(3+c)*(2-1);
z= a-(b/(3+c)*2)-1;
clrscr();
printf(“%f%f%f”,x,y,z);
}
Output: 10.000000
7.000000
4.000000

You might also like