<<

Type Conversion ,Type Casting, Operator Precedence and Associativity in

Gaurav Kr. suman 4/30/20 MAT09

The type conversion in C is basically converting one type of type to other to perform some operation. The conversion is done only between those datatypes wherein the conversion is possible There are two types of type conversion:

This type of conversion is usually performed by the when necessary without any commands by the user. Thus it is also called "Automatic Type Conversion". • Done by the compiler on its own, without any external trigger from the user.

• Generally takes place when in an expression more than one is present. In such condition type conversion (type promotion) takes place to avoid lose of data.

• All the data types of the variables are upgraded to the data type of the variable with largest data type.

Now, let’s focus on some examples to further understand about type conversions in C. Example 1

int a = 20; double b = 20.5; a + b;

Here, first operand is int type and other is of type double. So, as per rule, the variable a will be converted to double. Therefore, the final answer is double a + b = 40.500000.

Example 2 char ch='a'; int a =13;

a+c;

Here, first operand is char type and other is of type int. So, as per rule , the char variable will be converted to int type during the operation and the final answer will be of type int. We know the ASCII value for ch is 97. Therefore, final answer is a + c = 97 + 13 = 110.

For easy to understanding follow the flowchart given below, in which datatypes have been arranged in a hierarchy of highest to the lowest rank.

1 | P a g e

Explicit type conversion rules out the use of compiler for converting one data type to another instead the user explicitly defines within the program the datatype of the operands in the expression. This process is also called type casting and it is user defined. Here the user can type cast the result to make it of a particular data type.

Converting one datatype into another is known as type casting or, type-conversion. For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'. You can convert the values from one type to another explicitly using the cast operator Syntax:

(type_name) expression

2 | P a g e

without Type casting

#include main () { int a; a = 15/6; printf("%d",a); }

Output:

the above C program, 15/6 alone will produce integer value as 2.

After Type casting #include void main () { float a; a = (float) 15/6; printf("%f",a); }

Output

After type cast is done before division to retain float value 2.500000.

Operator precedence determines which operator is performed first in an expression with more than one operators with different precedence. For example 100-2*30 would yield 40, because it is evaluated as 100 – (2*30) and not (100-2)*30. The reason is that multiplication * has higher precedence than subtraction(-).

Operators Associativity is used when two operators of same precedence appear in an expression. Associativity can be either Left to Right or Right to Left.

3 | P a g e

For example multiplication and division arithmetic operators have same precedence, lets say we have an expression 5*2/10, this expression would be evaluated as (5*2)/10 because the associativity is left to right for these operators. Similarly 20/2*5 would be calculated as (20*2)/5.

OPERATOR DESCRIPTION ASSOCIATIVITY

( ) Parentheses (function call) left-to-right

[ ] Brackets (array subscript)

. Member selection via object name

-> Member selection via pointer

++ — Postfix increment/decrement

++ — Prefix increment/decrement right-to-left

+ – Unary plus/minus ! ~ Logical negation/bitwise (type) complement *

& Cast (convert value to temporary sizeof value of type)

Dereference

Address (of operand)

Determine size in on this

implementation

* / % Multiplication/division/modulus left-to-right

+ – Addition/subtraction left-to-right

<< >> Bitwise shift left, Bitwise shift right left-to-right

4 | P a g e

< <= Relational less than/less than or left-to-right

> >= equal to

Relational greater

than/greater than or equal to

== != Relational is equal to/is not equal left-to-right

to

& Bitwise AND left-to-right

^ Bitwise exclusive OR left-to-right

| Bitwise inclusive OR left-to-right

&& Logical AND left-to-right

| | Logical OR left-to-right

? : Ternary conditional right-to-left

= Assignment right-to-left

+= -= Addition/subtraction assignment

*= /= Multiplication/division assignment

%= &= Modulus/bitwise AND assignment

^= |= Bitwise exclusive/inclusive OR

<<= >>= assignment

Bitwise shift left/right assignment

, Comma (separate expressions) left-to-right

5 | P a g e