C PROGRAMMING NOTES , IOE
3.6. Evaluation and Type Conversion in Expressions
In C, type conversion, also known as type casting, refers to the process of changing the data type of a variable from one type to another. This is important when performing operations involving different data types or when assigning a value of one type to a variable of another type. Type conversion can be broadly categorized into two types: implicit (automatic) conversion and explicit (manual) conversion.
// c program to demonstrate implicit type casting
#include<stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character y
float z;
// y implicitly converted to int. ASCII value of 'a' is 97
x = x + y;
// x is implicitly converted to float
z = x + 1.0;
printf("\nx = %d, z = %f\n", x, z);
return 0;
}
(data type)expression;
// C program to demonstrate explicit type casting
#include<stdio.h>
int main()
{
float x = 1.2;
int sum;
// Explicit conversion from float to int
sum = (int)x + 1;
printf("sum = %d\n", sum);
return 0;
}
Type Promotion and Demotion:
Rules of Division in C Programming
Example C program to understand rules of division.
// c program to demonstrate concept of Rules of Division
#include<stdio.h>
int main()
{
int a = 2, b = 3,res;
res = a / b;
printf("\nres= %d\n", res);
return 0;
}
Output of above program is:
res= 0
Since operation is between integer variables, value after decimal point is omitted, which results in data loss, and it can be avoided by using explicit type casting as follows:
// c program to demonstrate concept of Rules of Division
#include<stdio.h>
int main()
{
int a = 2, b = 3;
float res;
res = (float)a / b;
printf("\nres= %f\n", res);
return 0;
}
Output of above program is:
res= 0.666667
Operator precedence and associativity
Operator Precedence
The data type and the value of an expression depends on the data types of the operands and the order of evaluation of operators which is determined by the precedence and associativity of operators. Let us first consider the order of evaluation. When expressions contain more than one operator, the order in which the operators are evaluated depends on their precedence levels.
For example:
Determine the hierarchy of operations and evaluate the following expression, assuming that i is an integer variable:
i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8
Stepwise evaluation of this expression is shown below:
i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8
i = 6 / 4 + 4 / 4 + 8 - 2 + 5 / 8 operation: *
i = 1 + 4 / 4 + 8 - 2 + 5 / 8 operation: /
i = 1 + 1+ 8 - 2 + 5 / 8 operation: /
i = 1 + 1 + 8 - 2 + 0 operation: /
i = 2 + 8 - 2 + 0 operation: +
i = 10 - 2 + 0 operation: +
i = 8 + 0 operation : -
i = 8 operation: +
Operator Associativity
When an expression in C contains more than one operator of equal/same priority, the tie of order of evaluation is broken by a concept called operator associativity. C programming language supports two types of operator associativity:
Let us consider the expression: x = 10/2*4
Here in the given expression operator / and * have same priority, the order of evaluation now is decided by Left to right associativity, hence we get result as:
x = 10/2*4
x= 5*4
x = 20
Let us consider another expression: x = y = 3
Here we have two = hence same priority, the order of evaluation for = follows right to left associativity.
The table below summarizes operator precedence and associativity:
Operators |
Description |
Associativity |
Precedence Rank |
() [] |
Function call Array element reference |
Left -> Right |
1 |
+ - ++ -- ! ~ * |
Unary Plus Unary minus Increment Decrement Logical negation Ones complement Pointer reference |
Right -> Left |
2 |
& sizeof |
Address Sizeof operator |
Right -> Left |
2 |
* |
Multiplication |
Left -> Right |
3 |
/ |
Division |
||
% |
Modulus |
||
+ |
Addition |
Left -> Right |
4 |
- |
Subtraction |
||
<< |
Leftshift |
||
>> |
Rightshift |
||
< |
Less than |
Left -> Right |
5 |
<= |
Less than or equal to |
||
> |
Greater than |
||
>= |
Greater than or equal to |
||
== |
Equality |
Left -> Right |
6 |
!= |
Inequality |
||
& |
Bitwise AND |
7 |
|
^ |
Bitwise XOR |
8 |
|
| |
Bitwise OR |
9 |
|
&& |
Logical AND |
10 |
|
|| |
Logical OR |
11 |
|
?: |
Conditional operator |
12 |
|
= += -= *= /= %= |
Assignment operator |
Right -> Left |
13 |
, |
Comma operator |
Left -> Right |
14 |
The above table clearly states the operator follows following order of evaluations:
C Program code to understand concept of operator precedence and associativity:
// c program to demonstrate concept of operator precedence and associativity
#include<stdio.h>
int main()
{
int res;
res = 2 + 4 > 3 && 5 == 5;
printf("Result=%d\n", res);
return 0;
}
Output of above code is:
Result=1
The above result can be explained as follows:
res = 2 + 4 > 3 && 5 == 5
res = 6 > 3 && 5 == 5
res = 1 && 5 == 5
res = 1 && 1
res = 1
the final result of the expression res = 2 + 4 > 3 && 5 == 5 is res = 1. The order of evaluation follows the operator precedence rules, where the addition (+) is performed first, followed by the relational (>, ==) and logical (&&) operations.