You are on page 1of 23

Operators and Expressions

-III 4

Chapter Outline
“First, master the fundamentals.”
–Larry Bird 5.1. Introduction.
5.2. Types of operators
5.2.1. Assignment
operator
5.2.2. Binary arithmetic
operators.
5.2.3. Unary arithmetic
“I long to accomplish great and noble task, but operators.
it is my chief duty to accomplish small tasks as
5.2.4. Relational
if they were great and noble.”
–Helen Keller operators.
5.2.5. Logical operators.
5.2.6. Conditional operator
5.2.7. Bitwise operators.
5.2.8. Special operators.
5.3. Operator’s
“Success is neither magical nor mysterious. Success
is the natural consequence of consistently applying precedence and
the basic fundamentals.” associativity.
–Jim Rohn
5.4. Evaluating an
expression.
5.5. Type conversion in an
expression.
5.1. Introduction
Operator: Operator is a special character (or symbol) that performs an operation using operands.
Operand: Operand is a variable or constant that acts as input for an operation to be performed.
Expression: Expression is a collection of operators and operands that, when evaluated, returns a value.
Ex: a+b is an expression in which the operator is + and the operands are a and b.
Unary operator: Unary operator is an operator that performs operation using only one operand at a
time.
e.g., ++ (increment) is a unary operator.
Binary operator: Binary operator is an operator that performs operation using two operands at a time.
e.g., > (greater than) is a binary operator.
Ternary operator: Ternary operator is an operator that performs operation using three operands at a
time.
e.g., ?:(Conditional operator) is ternary operator.

5.2. Types of operators


C has a rich set of operators. Based on the type of operation performed, operators are broadly classified
as follows:
1. Assignment operator.
2. Binary arithmetic operators.
3. Unary arithmetic operators.
4. Relational operators.
5. Logical operators.
6. Bitwise operators.
7. Special operators

5.2.1. Assignment operator


Assignment operator is used to assign (or give) a value to a variable or constant. The assignment operator
is = (can be read as “assigns to”). The assignment operator can be used to form an expression as follows:

Operand_1=Operand_2;

Here, Operand_1 (also called as LValue) can be a variable or constant. Operand_2 (also called as RValue)
can also be a variable or constant or an expression.

When assignment operator is used, the RValue gets assigned to LValue.


Ex:
1) a=10; /* value 10 gets assigned to variable a*/
2) const float PI=3.1412f; /*value 3.1412 gets assigned to constant PI*/
3) c=a+b; /* a+b is calculated and result gets assigned to c*/
4) a=b=c; /* multiple assignment: value of c gets assigned to b, which in turn gets assigned to a*/
5) a+b=10; /* invalid assignment statement*/
Program #1
Write a program to interchange (or swap) two values by using temporary variable.
/* A program to interchange two numbers using temporary variable*/
#include<stdio.h>
main()
{
long int a,b,temp;
printf(“\n Enter any two numbers:”);
scanf(“%ld%ld”,&a,&b);
printf(“\n Before swap a=%ld\tb=%ld”,a,b);
temp=a; /* First, assign value of a to temp*/
a=b; /* Later, assign value of b to a*/
b=temp; /* Finally, assign value of temp to b*/
printf(“\n After swap a=%ld\tb=%ld”,a,b);
}
Output:
Enter any two numbers: 2943
34646
Before swap a=2943 b=34646
After swap a=34646 b=2943

5.2.2. Binary arithmetic operators


C language provides operators for basic arithmetic operations such as addition, subtraction, multiplication
and division. There are five binary arithmetic operators and these operators act on two operands at a
time.
These are:
Operator Operation
+ Returns the sum of values of two operands.
- Returns the difference of values of two operands.
* Returns the product of values of two operands.
/ Returns the quotient after division of two operands.
% Returns the remainder after division of two operands.

These operators can be used to form an arithmetic expression as follows:

Operand_1 Binary_arithmetic_operator Operand_2

Ex: a+b, a-b, a*b, a/b, a%b


The above expressions are called as binary arithmetic expressions. The operands may be of any
one of data types. If both operands are integer values, then that expression is called as integer
arithmetic expression. If both are floating-point values, then that expression is called as real-
arithmetic expression. If both are of different values, then that expression is called as mixed-mode
arithmetic expression.
When binary operations are applied to numeric arguments of different types, numeric promotion
is performed before the operation takes place. The numeric promotion consists of converting the values of
the operands to a common type.
The rules for this type conversion are as follows:
 If one of the operands is a long double, then the other is converted a long double.
 Otherwise, if one of the operands is a double, then the other is converted to a double.
 Otherwise, if one of the operands is a float, then the other is converted to a float.
 Otherwise, if one of the operands is a long, then the other is converted to a long.
 Otherwise, if one of the operands is unsigned int, the other is converted to unsigned to int.
 Otherwise, both operands are converted to int values.
Ex: 2.3 (a double value) +3 (an int value) =5.3 (a double result)
The integer value in this expression gets promoted to double; addition is performed and the result is of
type double is returned.
Program #2
Write a program to perform all arithmetic operations
/*program to perform all arithmetic operations*/
#include<stdio.h>
main()
{
int a,b;
printf(“\nEnter any two numbers: ”);
scanf(“%d%d”,&a,&b);
printf(“\nAddition=%d”,a+b);
printf(“\nSubtraction=%d”,a-b);
printf(“\nMultiplication=%d”,a*b);
printf(“\nQuotient=%d”,a/b);
printf(“\nRemainder=%d”,a%b);
}
Output:
Enter any two numbers: 5
6
Addition=11
Subtraction=-1
Multiplication=30
Quotient=0
Remainder=5

Note:
1. The operator % does not work on the operands of data types: float or double or long double.
2. An unusual aspect of programming in C is the arithmetic which can be done with characters.
Consider the following:
char ch1,ch2;
ch1=’a’; /*assign ‘a’ to ch1*/
ch2=ch1+1; /*assign ‘b’ to ch2*/
The second assignment increases the ASCII (American Standard Code for Information Interchange) value
for the given character, a numeric value, by 1 and sets the result to ch2. Thus, ch2 contains ‘b’.
3. x%y is always equals to (x-(x/y)*y).
4. The second operand of operators % and / must be non-zero.
Program #3
Write a program to convert an alphabet from lowercase to uppercase
/*program to convert the lowercase to uppercase*/
#include<stdio.h>
main()
{
char ch;
printf(“\n Enter any lowercase alphabet:\n”);
scanf(“%c”,&ch); /* or ch=getchar();*/
ch=ch-32;
printf(“\n Uppercase alphabet:%c”,ch);
}

Output:
Enter any lowercase alphabet:
m
Uppercase alphabet:M

1)#include<stdio.h> 4)#include<stdio.h> 7)#include<stdio.h>


main() main() main()
{ { {
int x; printf(“%d”,4/3); float a=4;
x=-3+4-7*8/5%10; printf(“%d”,4/-3); int i=2;
OBSERVABL printf(“x=%d”,x); printf(“%d”,-4/3); printf(“%f%d”,i/a,i/a);
E } printf(“%d”,-4/-3); printf(“%d%f”,i/a,i/a);
} }

O 2)#include<stdio.h>
main()
5)#include<stdio.h>
main()
8)#include<stdio.h>
main()

U
{ { {
float a=1.5; printf(“%d”,4%3); int x;
int b=3; printf(“%d”,4%-3); x=4%5+6%5;

T
a=b/2+b*8/b-b+a/3; printf(“%d”,-4%3); printf(“x=%d”,x);
printf(“a=%f”,a); printf(“%d”,-4%-3); }
} }

P 3)#include<stdio.h>
main()
6)#include<stdio.h>
main()
9)#include<stdio.h>
main()
{ {

U
{ float a=5,b=2; int x;
int x; int c; x=3*4%5;
x=-3*-4%-6/-5; c=a%b; printf(“x=%d”,x);

T
printf(“x=%d”,x); printf(“%d”,c); }
} }
5.2.3. Unary arithmetic operators
Unary Arithmetic operators perform the arithmetic operations such as increment & decrement and change
of sign on only one operand. There are 4 unary arithmetic operators:
Operator Operation
Unary + (Positive) Keeps the sign of value unchanged.
Unary - (Negative) Changes the sign of value.
++ (Increment) Adds 1 to value of operand and assigns the result to that operand only.
- - (Decrement) Subtracts 1 from the value of operand and assigns the result to that operand
only.

These operators can be used to form an expression as follows:

Operand_1 Unary_arithmetic_operator
(or)
Unary_arithmetic_operator Operand_1

Both sign operators should be preceded by the operand where as the increment and decrement operators
can be preceded or followed by the operand.
Ex: int a=10;
1) +a /*keeps the sign of 10 as positive only*/
2) –a /* changes the sign of 10 to negative*/
3) ++a or a++ /* equivalent to a=a+1; hence, a holds 11*/
4) –-a or a-- /* equivalent to a=a-1; hence, a holds 9*/
Program #4
Write a program to demonstrate positive and negative signs
/*program to demonstrate positive and negative signs*/
#include<stdio.h>
main()
{
int num;
printf(“\n Enter any number:”);
scanf(“%d”,&num);
printf(“\n Positive number=%d”,+num);
printf(“\n Negative number=%d”,-num);
printf(“\n Positive number=%d”,+(+num));
printf(“\n Negative number=%d”,+(-num));
printf(“\n Positive number=%d”,-(-num));
printf(“\n Negative number=%d”,-(+num));
}
Output:
Enter any number: 123
Positive number=123
Negative number=-123
Positive number=123
Negative number=-123
Positive number=123
Negative number=-123
Few words about increment and decrement operators:
If ++ is used before the operand, then this increment operator is called as pre-increment operator. If +
+ is used after the operand, then this increment operator is called as post-increment operator. The
same treatment will be given to the --.

The increment / decrement operator works as a simple increment / decrement by one, if it is not
used as a part of an expression. In that case, there is no difference between operations of pre or post
increment or decrement operators.

Ex: int a=10; Ex: int a=10;


a++; ++a;
printf(“a=%d”,a); printf(“a=%d”,a);
Output: a=11 Output: a=11

However, when these operators are used as a part of an expression then the difference can be
noticed:
 The pre-increment operator first increments the value of operand and then returns that
incremented value.
 The post-increment operator first returns the existing value of operand and then increments the
value of the operand.
 The pre-decrement operator first decrements the value of operand and then returns that
decremented value.
 The post-decrement operator first returns the existing value of operand and then decrements the
value of the operand.

Ex: int a=10; Ex: int a=10;


x=50+a++; x=50+(++a);
printf(“x=%d,a=%d”,x,a); printf(“x=%d,a=%d”,x,a);
Output: x=60,a=11 Ouput: x=61,a=10
1)#include<stdio.h> 4)#include<stdio.h> 7)#include<stdio.h>
main() main() main()
{ { {
int x=5; int x=3,z; int x=3,z;
x++; z=x---111; z=x++ + x++;
printf(“x=%d”,x); printf(“x=%d z=%d”,x,z); printf(“x=%d z=
++x; %d”,x,z);
printf(“x=%d”,x); } }

} 5)#include<stdio.h> 8)#include<stdio.h>
OBSERVABL
main() main()
E 2)#include<stdio.h> { {

O
main() int x=3,z; int x=3,z;
{ z=x--- -1; z=x++ + ++x;
int x=3,z; printf(“x=%d z=%d”,x,z); printf(“x=%d z=

U
z=x++ +10; %d”,x,z);
printf(“x=%d z=%d”,x,z); } }
}
6)#include<stdio.h> 9)#include<stdio.h>

T
3)#include<stdio.h>
main() main() main()
{ { {
int x=3,z; int x=3,z; int x=3,z;

P z=++x +10;
printf(“x=%d z=%d”,x,z);
z=x----1;
printf(“x=%d z=%d”,x,z);
z=x/++x;
printf(“x=%d z=
%d”,x,z);

U 10)#include<stdio.h> 11)#include<stdio.h> 12)#include<stdio.h>

T
main() main() main()
{ { {
int i=3,j; int x=4,y=3,z; int a,b;
j=++i*++i*++i; z=x-- -y; a=-3 - -3;
printf(“i=%d j=%d”,i,j); printf(“x=%dy=%dz= b=-3 - -(-3);
%d”,x,y,z); printf(“%d%d”,a,b);
} } }

5.2.4. Relational operators


The relational operators are used to compare two operands. There are six relational operators available.
All these operators are binary operators. These are used to form an expression that is either true or false.
The following table lists all the relational operators.
Operator Operation
== (is equal to) Checks whether first operand is equal to second operand or not.
!= (is not equal to) Checks whether first operand is not equal to second operand or not.
> (is greater than) Checks whether first operand is greater than the second operand.
>=(is greater than or Checks whether first operand is greater or equals to second operand.
equal to)
< (is lesser than) Checks whether first operand is lesser than the second operand.
<= (is lesser than or Checks whether first operand is lesser or equals to the second operand.
equals to)

A relational expression is of the following form:

Operand_1 relational_operator Operand_2


Where operand_1 and Operand_2 are variables or constants or arithmetic expressions.
Ex: int i=7; Expression Value
float f=5.5; f>5 1 (true)
(i+f)<=10 0 (false)
char c=’w’;
c==119 1 (true)
c!=’p’ 1 (true)
c>=10*(i+f) 0 (false)

Program #4
Write a program to demonstrate relational operators
/*program to demonstrate relational operators*/
#include<stdio.h>
main()
{
int num1,num2;
num1=20;
num2=30;
printf(“\n Is equal to condition=%d”,(num1==num2));
printf(“\n Is not equal to condition=%d”,(num1!=num2));
printf(“\n Is greater than condition=%d”,(num1>num2));
printf(“\n Is greater than or equal to condition=%d”,(num1>=num2));
printf(“\n Is lesser than condition=%d”,(num1<num2));
printf(“\n Is lesser than or equal to condition=%d”,(num1<=num2));
}
Output:
Is equal to condition=0
Is not equal to condition=1
Is greater than condition=0
Is greater than or equal to condition=0
Is lesser than condition=1
Is lesser than or equal to condition=1

5.2.5. Logical operators (or) Compound relational operators


The logical operators are used to combine the relational expressions and evaluate them. There are three
logical operators. These work on the values of operands that are either true or false. Both Logical AND and
Logical OR operators are binary operators whereas Logical NOT is only unary operator. The following table
lists them:

Operator Operation
&& (Logical AND) Performs Logical AND operation on two operands.
|| (Logical OR) Performs Logical inclusive OR operation on two operands.
! (Logical NOT) Reverses the value of operand.
The logical operators are used to form logical expressions as shown below:
1) Logical AND expression:

Expression_1 && Expression_2

Where Expression_1 and Expression_2 are any expression those return either true or false.
When logical AND operator is encountered, first Expression_1 is tested. It returns either true or
false. Secondly, the expression_2 is tested. It returns either true or false. Then both of these expressions’
values are combined to give the value of logical expression as given below:
Expression_1 Expression_2 Expression_1 && Expression_2
1 1 1
1 0 0
0 1 0
0 0 0
Ex: int a=10,b=30,c;
c=((a>b)&&(a!=b)); Ans: c=1
Note: If the left operand yields false value, the right operand is not evaluated by a compiler in a logical
expression using &&.
2) Logical OR expression:

Expression_1 || Expression_2

Where Expression_1 and Expression_2 are any expression those return either true or false.
When logical AND operator is encountered, first Expression_1 is tested. It returns either true or
false. Secondly, the expression_2 is tested. It returns either true or false. Then both of these expressions’
values are combined to give the value of logical expression as given below:
Expression_1 Expression_2 Expression_1 || Expression_2
1 1 1
1 0 1
0 1 1
0 0 0
Ex: int a=10,b=30,c;
c=((a>b)||(a!=b)); Ans: c=1
Note: If the left operand yields true value, the right operand is not evaluated by a compiler in a logical
expression using ||.
3) Logical NOT expression:

!(Expression_1)

where Expression_1 is any expression that returns either true or false.


When logical NOT operator is encountered, first the Expression_1 is tested. It returns either true or
false. Upon the value of this Expression_1, Logical NOT returns the value given below:
Expression_1 !(Expression_1)
1 0
0 1
Ex: int a=20,b=30,c=40,d;
d=!(a>b&&b>c); ans: d=1

1)#include<stdio.h> 4)#include<stdio.h> 7)#include<stdio.h>


main() main() main()
{ { {
int x,y,z; int x,y,z; int x=10,y=5,p,q;
x=y=z=1; x=y=z=-1; p=x>9;
z=++x||++y&&++z; z=++x||++y&&++z; q=x>3&&y!=3;
printf(“%d %d %d”,x,y,z); printf(“%d %d %d”,x,y,z); printf(“%d %d”,p,q);
} } }

2)#include<stdio.h> 5)#include<stdio.h> 8)#include<stdio.h>


OBSERVABL main() main() main()
E { { {

O
U 3)#include<stdio.h> 6)#include<stdio.h>
9)#include<stdio.h>

T
main()
main() main()
{
{ {
int a=100,b=200,c;
int x,y,z; int x,y,z;
c=(a==100||b>200);

P
x=y=z=1; x=y=z=-1;
printf(“c=%d”,c);
z=++x&&++y||++z; z=++x&&++y||++z;
}
printf(“%d %d %d”,x,y,z); printf(“%d %d %d”,x,y,z);

U
} }

10)#include<stdio.h> 11)#include<stdio.h> 12)#include<stdio.h>

T
main() main() main()
{ { {
int x=11,y=6,z; int x=10,y=-20; int x=0,y=1;
z=x==5||y!=4; x=!x; y=!x;
printf(“z=%d”,z); y=!y; x=!y;
} printf(“%d %d”,x,y); printf(“%d %d”,x,y);
} }
5.2.6. Conditional operator
The conditional operator consists of two symbols: the question mark (?) and the colon (:). This is the only
operator in C that acts on three operands at a time. Hence, this operator is also called as ternary operator
or trinary operator. This operator acts in the same way as the if…else statement acts. Conditional
operator can be used as follows:

Condition? Expression_1: Expression2;


In this syntax,
Condition is any expression to be tested and it returns either true or false. Usually, it is either a relational
expression or compound relational expression.
Expression_1 and Expression_2 are any statements that are to be executed.
When the conditional operator is encountered, first, the Condition is tested. It returns either
true or false. If the condition is true, Expression_1 is executed. If the condition is false, Expression_2
is executed. This is shown as in the following chart:

False True

Condition

Expression_2 Expression_1

Next_statement

Program #4
Write a program to find biggest of three numbers
/*program to find biggest of three numbers*/
#include<stdio.h>
main()
{
int num1,num2,num3,big;
printf(“\n Enter any three numbers:”);
scanf(“%d%d%d”,&num1,&num2,&num3);
big=((num1>num2)&&(num1>num3)?num1:num2>num3?num2:num3;
printf(“\n Biggest of three numbers=%d”,big);
}
Output:
Enter any three numbers: 190
452
43
Biggest of three numbers=452
1)#include<stdio.h> 4)#include<stdio.h>
main() main()
{ {
int x=3,y=4,z=4; int k=12,n=30;
printf(“ans=%d”,z>=y&&y>=x?1:0); k=(k>5&&n=4?100:200);
OBSERVABL } printf(“k=%d”,k);
E }

O 2)#include<stdio.h>
main() 5)#include<stdio.h>
main()

U
{
int x=3,y=4,z=4; {
printf(“ans=%d”,z>=y>=x?1:0); int c=0,d=5,e=10,a;
} a=c>1?d>1||e>1?100:200:300;

T printf(“a=%d”,a);
}
3)#include<stdio.h>

P main()
{
int i=-4,j,num=10;
6)#include<stdio.h>
main()

U
{
j=i%-3;
int a=10,b=10;
j=(j?0:num*num);
printf(“ans=%d”,a>b?a*a:b/b);
printf(“j=%d”,j);
}

T
}

5.2.7. Bitwise operators

Bitwise operators perform operations on bits of data. These operators are used for testing, complementing
or shifting bits to the right or left. Usually, bitwise operators are not useful in cases of float and double.
There are six bitwise operators as shown below:

Operator Operation
& (Bitwise AND) Returns 1 if both corresponding bits are 1; otherwise 0.
| (Bitwise inclusive OR) Returns 0 if both corresponding bits are 0; otherwise 1.
^(Bitwise exclusive OR) Returns 0 if both corresponding bits are 0 or 1; otherwise 1.
~ (One’s complement) Returns 1, if bit is 0 and returns 0, if bit is 1.
<< (Left shift) Shifts least significant bit by specified number of times.
>> (Right shift) Shifts most significant bit by specified number of times.

All these operators are binary operators except one’s complement operator. One’s complement operator is
a unary operator.
Bitwise AND (&):
The operator & performs a bitwise AND between two operands. It compares each bit of the left operand
with the corresponding bit of the right operand. For each bit, the result is 1, if both the compared bits are
1; otherwise, the result is 0 as shown below:

Compared bits Result


0&0 0
0&1 0
1&0 0
1&1 1

Interview question #1

What is masking?
Masking is an operation in which the desired bits of a binary number or bit pattern are set to zero. The
operator & (Bitwise AND) is very useful for this purpose. To mask particular bits in a binary number, do
the following:
1. Create a new number that has binary pattern with 0s in the positions that you want to mask and
1s in other positions.
2. Perform bitwise AND operation between the number that is to be masked and created number.

Bitwise Inclusive OR (|)


The operator | performs a bitwise inclusive OR between two operands. It compares each bit of the left
operand with the corresponding bit of the right operand. For each bit, the result is 0, if both the compared
bits are 0; otherwise, the result is 1 as shown below:

Compared bits Result


0|0 0
0|1 1
1|0 1
1|1 1

Bitwise Exclusive OR (^)


The operator ^ performs a bitwise exclusive OR between two operands. It compares each bit of the left
operand with the corresponding bit of the right operand. For each bit, the result is 0, if both the compared
bits have the same value; otherwise, the result is 1 as shown below:

Compared bits Result


0^0 0
0^1 1
1^0 1
1^1 0

Bitwise Right shift operator (>>)


The right shift operator >> moves the bits to the right based on specified number of positions. The
general form of right shift expression is:

Constant >> n
(or)
variable >>n

where n is the number of bit positions to be shifted.


Ex: int a=10;
x=a>>3;
The right shift operator shifts the bits in the variable ‘a’ thrice.

Bitwise Right shift operator (<<)


The left shift operator << moves the bits to the left based on specified number of positions. The general
form of left shift expression is:

Constant << n
(or)
variable <<n

where n is the number of bit positions to be shifted.


Ex: int a=10;
x=a<<3;
The left shift operator shifts the bits in the variable ‘a’ thrice.

One’s complement operator (~)


One’s complement operator is a unary operator. It acts on one operand at a time. One’s complement
operator converts all the 1-bits to 0 and all 0-bits to 1 of the binary pattern of the operand. The one’s
complement operator should be preceded with operand as follows:

~operand1

Where operand1 is a variable or a constant.


Ex: int a=10;
x=~a;
Interview question #2
What are uses of shift operators?
 To divide an integer by 2n, a right shift by n bit positions is applied.
 To multiply an integer by 2n, a left shift by n positions is applied.

Program #5
Write a program to convert an uppercase alphabet to lowercase alphabet
/*program to convert an uppercase alphabet to lowercase alphabet*/
#include<stdio.h>
int main()
{
char alpha;
printf(“\n Enter any uppercase alphabet:”);
scanf(“\n%c”,&alpha);
printf(“\n Lower case alphabet=%c”, alpha | 32);
return 0;
}
Output:
Enter any uppercase alphabet:
A
Lower case alphabet=a

Program #6
Write a program to swap two integers with out using temporary variable
/*program to swap two integers without using temporary variable*/
#include<stdio.h>
int main()
{
int a,b;
printf(“\n Enter any two numbers:”);
scanf(“%d%d”,&a,&b);
printf(“\n Before swap a=%d\tb=%d”,a,b);
a=a^b;
b=a^b;
a=a^b;
printf(“\n After swap a=%d\tb=%d”,a,b);
return 0;
}
Output:
Enter any two numbers: 10
20
Before swap a=10 b=20
After swap a=20 b=10
5.2.8. Special operators
1. sizeof operator.
2. comma operator.
3. short-hand operators.
4. Referencing and dereferencing operators.

sizeof operator: It is an unary operator. It returns the number of bytes occupied by the operand in
memory. The operand may be a variable, a constant, an expression or simply a datatype.
Ex: printf(“%d”,sizeof(10)); /*prints 2 or 4*/
printf(“%d”,sizeof(int)); /*prints 2 or 4*/
int x=30; float y=45.355;
printf(“%d”,sizeof(y)); /*prints 4*/
printf(“%d”,sizeof(x+y)); /*prints 4*/

Comma Operator: A comma operator can be used as a separator or a terminator in the following
statements:
int a,b,c;
c=a,a=b,b=c;
This operator can also be used to link related expressions together. A comma-linked list of expressions
is evaluated from left to right and the value of right-most expression is the value of combined
expression.
Ex: value=(x=10,y=20,x+y); ans: value=30

Short-hand Operators: These operators are formed by combining both binary arithmetic operators
and bitwise operators with assignment operator. Hence, these are also called as compound assignment
operators.
The short-hand operators include: +=, -=, *=, /=, %=
&=, |=, ^=, <<=, >>=.
All of these operators are binary operators. So, these operands should act on two operands at a time.

Operand_1 short-hand operator Operand_2

Usually, Operand_1 should be a variable and Operand_2 can be a variable or a constant.


When compound assignment operator is encountered, the desired operation is performed between
Operand_1 and Operand_2 and the result gets stored in Operand_1.
Ex: a+=2 means a=a+2
int a=20;
a+=2; ans: a=22
Refencing and dereferencing operators: Referencing operator (&) is the operator that returns the
address of operand in the memory. Dereferencing operator (*) is the operator that returns the value at
the address that is pointed by the referencing operator.
Ex: int a=10;
printf(“\n Address of a=%u”,&a); /*prints address of a*/
printf(“\n Value of a=%d”,*(&a)); /*prints value of a */

1) Are the following two statements same (yes /no)


a<=20?b=30:c=20;
(a<=20)?b:c=30;
2) We want to round of x, a float to an int value. The correct way to do so
OBSERVABL
would be:
E
a) y=(int) (x+0.5);

O b) y=int (x+0.5);
c) y=(int )x+0.5;

U d) y=(int)((int)x+0.5)

T
1)#include<stdio.h>
main()
{

P
printf(“%d %d %d”,sizeof(3.14f),sizeof(3.14),sizeof(3.14l));
}

U
2)#include<stdio.h>
main()
{

T
printf(“%d”,sizeof(4)/sizeof(2.0));
printf(“%d”,sizeof(2.0)/sizeof(4));
}

5.3 Operators’ precedence and associativity


Precedence of an operator is the priority of it in the evaluation of an expression.
Associativity of an operator is its order of evaluation in an expression. The evaluation continues
according to the associativity individually as well as when all the operators in the expression have the
same precedence.
The following table lists the operators, their precedence and associativity.
Precedence Operands Operators Associativity

1 2 () [] . -> Left-to-Right

2 1 ! ~ ++ -- + - * & Right-to-Left
(type) sizeof
3 2 * / % Left-to-Right

4 2 + - Left-to-Right

5 2 << >> Left-to-Right

6 2 < <= > >= Left-to-Right

7 2 == != Left-to-Right

8 2 & Left-to-Right

9 2 ^ Left-to-Right

10 2 | Left-to-Right

11 2 && Left-to-Right

12 2 || Left-to-Right

13 3 ? : Left-to-Right
14 2 = *= /= %= += -= Right-to-Left
&= |= ^= <<= >>=
15 2 , Left-to-Right

5.4. Evaluating an expression


Evaluation of an expression is the process of calculating the result from it based on operators’ precedence
and associativity.

5.5 Type conversion in an expression


Type conversion is the process of converting a value from one data type to another. This type conversion
makes the operands in an expression belong to similar data type.
If casting is done from lower size type to higher size type value, then it is called as broadening
conversion. E.g., char to int.
If casting is done from higher size type to lower size type value, then it is called as narrowing
conversion, causes some loss of value.
E.g. float to int causes truncation of fractional part.
float to long causes truncation of fractional part.
double to float causes rounding of digits.
long int to int causes dropping of the excess high order bits.
int to short causes dropping of the excess high order bits.

long double

double
This type conversion can be done either implicitly or explicitly.
Implicit type conversion: C permits mixing of constants and variables of different data types in an
expression. C automatically converts any intermediate values to the proper type so that expression can be
evaluated with out loosing any significance. This automatic conversion is known as implicit type
conversion.
During evaluation, the lower type is automatically converted to the higher type, according to strict
rules of type conversion. The rules for this type conversion are as follows:
1. If one of the operands is a long double, then the other is converted a long double.
2. Otherwise, if one of the operands is a double, then the other is converted to a double.
3. Otherwise, if one of the operands is a float, then the other is converted to a float.
4. Otherwise, if one of the operands is an unsigned long, then the other is converted to an unsigned
long.
5. Otherwise, if one of the operands is a long, then the other is converted to a long.
6. Otherwise, if one of the operands is unsigned int, the other is converted to unsigned int.
7. Otherwise, both operands are converted to int values.

Ex: int i=10,x;


float f=3.4f;
double d=7.4;
long int l=214L;
x=l/i+i*f-d
Note: If the two operands in an assignment statement are of different data types, the right side operand
is automatically converted to the data type of the left side.
Ex: int a=10; Ex: float a=10.98;
float b; int b;
b=a; b=a;
printf(“%f”,b); printf(“%d”,b);
output: output:
Program #4
Write a program to print ASCII value of a character
/*program to print ASCII value of a character*/
#include<stdio.h>
main()
{
char ch;
int val;
printf(“\n Enter any character:”);
scanf(“\n%c”,&ch);
val=ch;
printf(“\n ASCII value of \’%c\’ is %d”,ch,val);
}
Output:
Enter any character:
a
ASCII value of ‘a’ is 97

1)#include<stdio.h>
main()
{
printf(“%d %d %d”,72,072,0x72);
}
OBSERVABL
E
O
U 2)#include<stdio.h>
main()
T
{
P printf(“%d %o %x”,72,72,72);
U }
T

Explicit type conversion: When we want to do type conversion forcefully, then we use cast operator.
Cast operator is used to convert value of an operand from one type to another explicitly by the user. This
explicit type conversion is also called as casting or coercion.
The usage of cast operator is as follows:

(type_name) expression
Here, type_name is any standard data type. The expression may be a constant, variable or an expression.
As this operator works on only one operand at a time, this is a unary operator.
Ex: int a=10,b=25,c=32; Ex: int a=10,b=25,c=32;
float d; float d;
d=(a+b+c)/3; d=(float)(a+b+c)/3;
printf(“%f”,d); printf(“%f”,d);
output: 22.000000 output: 22.333334
From the above example, it is clear that the coercion helps us to avoid truncation in division.

5.6. Conclusion
C supports arithmetic, relational and logical operators like other programming languages. It supports
increment and decrement operators for faster execution. Bitwise operators are supported in C, which are
not available in other languages. In addition to simple assignments, C also provides compound
assignments or short-hand assignments.
Expressions are formulated with the help of operators and operands. These are evaluated to get
the desired result. These are evaluated according to the precedence levels of the operators and their
associativity. In evaluating mixed-type expressions, implicit conversion rules are followed. Explicit type
conversions are possible with coercion or type casting.

You might also like