C Operators Precedence and Type Conversions

Operators Precedence and Associativity

Operators precedence tells that if in an expression we have more than one operators then which operator is going to preceded by another or which operator is going to be executed before then Other operators. Precedence simply means the order in which operators will be executed.

For example like in mathematics we have BODMAS rule for solving mathematical problems similarly, C language has its own rule which decides the precedence of operators in C.

Let's take an example of an expression :
2 + 3 * 5

Here we have two operators - addition and multiplication operators. If addition is performed before the multiplication then result will be 25 and if multiplication is performed before addition the end result will be 17.
In C language multiplication operator has higher precedence than the addition operator, therefore the result will be 17.

Operator Precedence

Below is the table for operators precedence in C language :

Category
Operator (Higher to Lower) ↠↠↠↠↠
Associativity
Postfix
() [] -> . ++ - -
Left to right
Unary
+ - ! ~ ++ - - (type)* & sizeof
Right to left
Multiplicative
* / %
Left to right
Additive
+ -
Left to right
Shift
<< >>
Left to right
Relational
< > <= >=
Left to right
Equality
== =!
Left to right
Bitwise Operators
&   ^    |
Left to right
Logical Operators
&&   ||   
Left to right
Conditional Operator
? :
Right to Left
Assignment Operators
=  +=   -=   *=   /=
Right to Left

A simpler version of the above table is :

Category
Operator (Higher to Lower)
Associativity
Postfix
() [] -> . ++ - -
Left to right
Multiplicative
* / %
Left to right
Additive
+ -
Left to right
Relational
< > <= >=
Left to right
Equality
== =!
Left to right
Logical Operators
&&   ||   
Left to right
Assignment Operators
=  +=   -=   *=   /=
Right to Left

Let's understand the above table of Operator Precedence. The operators written in the Category column shows the precedence. The Operators precedence are decreasing on going below. Thus, Postfix has the highest priority and Assignment operators have the lowest priority or precedence. Also, In the postfix row :

Postfix
() [] -> . ++ - -
Left to right

'()' will hold the highest priority or precedence, next are '[]' and so on. Thus, decrement operator '--' holds the lowest priority or precedence in first row. In the Operator (Higher to Lower) column, precedence decreases on going 'right to left'.

Associativity

Associativity the direction in which the expression is going to be executed. Generally every expression is executed from left to right but there are also a few exceptions to it as shown in the table above.

Program Example :
#include <stdio.h>
int main(void){
    int a,b,c,d;
    a=10, b=5, c=15, d=25;
    
    int result = a+b*c/d ;
    printf("result of a+b*c/d is %d",result);
    /* a+b*c/d can be rewritten as
        = a + [(b*c) / d]
        = 10 + [(5*15) / 25]
        = 10 + [75 / 25]
        = 10 + 3
        = 13
    */
   
    result = a+b*(c/d) ;
    printf("\nresult of a+b*(c/d) is %d",result);
    /* a+b*(c/d) can be rewritten as
        = a + [b * (c/d)]
        = 10 + [5 * (15/25)]
        15/25 will result in 0.6, as result is an integer, it will hold only 0 not 0.6
        = 10 + [75 * 0]
        = 10 + 0
        = 10
    */       
}

Type Conversions in C 

As you know, C language uses 4 primitive data types which are int, char, float and double. We can use all these data types together in an expression. While using them in in any expression these data types needs to be converted into different data type. The conversion of one type of data into another type of data is known as Type Conversion.
For Example -
float marks1, marks2, marks3;
/* marks1, marks2, marks3 will hold marks of any student */


int sum;
marks1=20.50;
sum = marks1 + marks2 + marks3 ;


/* summation of marks */

marks2 = 35.57;
marks3 = 25.87;

/* sum = 20.50 + 35.57 + 25.87;
  sum = 81.94 ; As sum is of int type, it will not accept decimal numbers
   sum = 81
*/
/* sum is of int type, whereas marks1, marks2, marks3 are of float type. Thus, above statement converts float type to int */
/* We are trying to convert float data type to int data type. This conversion is known as Type Conversion */

printf("Total Marks : %d",sum);
// outputs 81

Type Conversions in C can be classified mainly of two type :

(i) Implicit Type Conversion

This type of conversions are automatically done by the compiler, you do not need to convert manually one type into another data type. The above shown is an example of implicit type of conversion.
Let's see one more example -
    char c1, c2, c3, c4;
    c1='A';
    c2='B';
    c3='H';
    c4='I';
    int var;
    
 /* Converting Alphabetical letters to its equivalent integer numbers */
    
var = c1; 
/* c1 char data type is converted to int data type*/
printf("A is equivalent to : %d\n",var);
//OUTPUTS 65
    
var = c2;
/* c2 char data type is converted to int data type*/
printf("B is equivalent to : %d\n",var);
//OUTPUTS 66
    
var = c3;
/* c3 char data type is converted to int data type*/
printf("H is equivalent to : %d\n",var);
//OUTPUTS 72
    
var = c4;
/* c4 char data type is converted to int data type*/
printf("I is equivalent to : %d\n",var);
//OUTPUTS 73
    
/* Converting Numbers to equivalent Characters */
printf("Converting integers to Character : %c%c%c%c\n",65,66,72,73);
//OUTPUTS ABHI
}

(ii) Explicit Type Conversion

Using the Type Casting Concept in our program :
z = x/y ;
z = (float)x/y;

There are some situations in which implicit type of conversion does not solve our purpose therefore we need to manually convert one data type into another data type. Such type of Conversion of one data type into another data type are known as Explicit Type Conversion.

For Example -
float z;
int x =15, y = 7;

z = x/y ;
/* Without type conversion, z will hold 2 instead of 2.142 */
The above problem can be handled by using Explicit Type Conversion. By using Explicit Type Conversion, we can convert one data type into another data type. This is known as Type Casting. 
Type Casting is done with the help of Cast Operator.
The syntax of cast operator :
(datatype) expression;
For Example -
(int)20.3
This will convert 20.3 (floating number) to integer and results in 20.

Let's take another example - 
(float)20/6
This will convert expression's '20/6' result to float type and results in "3.33". Otherwise, result would be 3.

float z;
int x =15, y = 7;

/* z holds value 2*/
/* Without type conversion, z will hold 2 instead of 2.142 */
printf("Before using Type Casting z = %f ",z);

/* z holds value 2.142*/
/* With type casting, z will hold 2.142 */
printf("After using Type Casting z = %f ",z);

Click on Run




Post a Comment