Type Conversion
Precedence & Associativity
- Aakash Singh
C
Learners
Type Conversion
 What is Type Conversion?
• Type Casting or Type Conversion is a way to
convert a variable from one data type to another
data type.
• For example, if you want to store a long value
into a simple integer then you can type cast long
to int.
2
Type Conversion
 Types of Type Conversion
• There are two types of type conversion –
1. Implicit Type Conversion
2. Explicit Type Conversion
3
1. Implicit Type Conversion
 What is Implicit Type Conversion?
• C automatically converts any intermediate values
to the proper type so that the expression can be
evaluated without losing any significance.
• This automatic conversion is known implicit type
conversion.
• If the operands are of different types, the ‘lower
type’ is automatically converted to the ‘higher’
type before the operation proceeds.
• The result is of the higher type.
4
Implicit Type Conversion
short char
int
Unsigned int
long int
Unsigned long int
float
double
long double
Conversion
Hierarchy
C uses rule that, in all expressions except assignments, any implicit type
conversions are made from a lower size to a higher size type as shown
below.
This hierarchy
can be used
while evaluating
expressions.
5
Example of Implicit Type Conversion
6
Example of Implicit Type Conversion
7
1. Explicit Type Conversion
 What is Explicit Type Conversion?
• The type conversion performed by the
programmer by posing the data type of the
expression of specific type is known as explicit
type conversion.
• The general form of a cast is :
(type-name) expression
• Here, ‘type-name’ is one of the standard C data
types and the expression may be a constant,
variable, or an expression.
8
Use of Explicit Type Casting
9
Example Action
X=(int)7.5
7.5 is converted to integer by
truncation.
a = (int)21.3/(int)4.5
Evaluated as (21/4) and the result
would be 5.
b = (double)sum/n Division is done in floating point mode.
y = (int)(a+b)
The result of (a+b) is converted to
integer.
z = (int)a+b
a is converted to integer and then
added to b.
p = cos((double)x) Converts x to double before using it.
Example of Explicit Type Conversion
10
Example of Explicit Type Conversion
11
Precedence & Associativity
 What is Precedence?
• C has a precedence associated with it. This
precedence is used to determine how an expression
involving more than one operator is evaluated.
 What is Associativity?
• The operators at higher level of precedence are
evaluated first.
• The operators of the same precedence are evaluated
either from ‘left to right’ or from ‘right to left’
depending on the level. This is known as the
associativity property of an operator.
12
Precedence & Associativity
13
 Precedence rules decides the order in which
different operators are applied.
 Associativity rule decides the order in which
multiple occurrences of the same level operator
are applied.
 C Operators with their Precedence & Associativity
are listed in the following table.
14
Operator Description Associativity Rank
()
[]
Function call
Array element
reference
Left to right 1
+
-
++
--
!
~
*
&
sizeof
(type)
Unary plus
Unary minus
Increment
Decrement
Logical Negation
Ones complement
Pointer reference
Address
Size of an object
Type cast
Right to left 2
*
/
%
Multiplication
Division
Modulus
Left to right 3
+
-
Addition
Subtraction
Left to right 4
<<
>>
Left shift
Right shift
Left to right 5
15
Operator Description Associativity Rank
<
<=
>
>=
Less than
Less than or equal to
Greater than
Greater than or equal
to
Left to right 6
==
!=
Equality
Inequality
Left to right 7
& Bitwise AND Left to right 8
^ Bitwise XOR Left to right 9
| Bitwise OR Left to right 10
&& Logical AND Left to right 11
|| Logical OR Left to right 12
?: Conditional expression Right to left 13
16
Operator Description Associativity Rank
=
*= /= %=
+= -= &=
^= |=
<<= >>=
Assignment
operators
Right to left 14
, Comma operator Left to right 15
Summary of operator precedence
17
 ‘Comma’ operator has lowest precedence.
 ‘Unary Operators’ are operators having highest
precedence.
 Operators sharing common block in the above
table have equal priority or precedence.
 While solving expression, equal priority operators
are handled on the basis of FIFO [First in First Out]
i.e., Operator Coming First is handled First.
Example of Precedence
18
Example of Associativity
19
20
Please give your precious feedbacks and suggestions at
aakashsingh07041997@gmail.com

Type Conversion, Precedence and Associativity

  • 1.
    Type Conversion Precedence &Associativity - Aakash Singh C Learners
  • 2.
    Type Conversion  Whatis Type Conversion? • Type Casting or Type Conversion is a way to convert a variable from one data type to another data type. • For example, if you want to store a long value into a simple integer then you can type cast long to int. 2
  • 3.
    Type Conversion  Typesof Type Conversion • There are two types of type conversion – 1. Implicit Type Conversion 2. Explicit Type Conversion 3
  • 4.
    1. Implicit TypeConversion  What is Implicit Type Conversion? • C automatically converts any intermediate values to the proper type so that the expression can be evaluated without losing any significance. • This automatic conversion is known implicit type conversion. • If the operands are of different types, the ‘lower type’ is automatically converted to the ‘higher’ type before the operation proceeds. • The result is of the higher type. 4
  • 5.
    Implicit Type Conversion shortchar int Unsigned int long int Unsigned long int float double long double Conversion Hierarchy C uses rule that, in all expressions except assignments, any implicit type conversions are made from a lower size to a higher size type as shown below. This hierarchy can be used while evaluating expressions. 5
  • 6.
    Example of ImplicitType Conversion 6
  • 7.
    Example of ImplicitType Conversion 7
  • 8.
    1. Explicit TypeConversion  What is Explicit Type Conversion? • The type conversion performed by the programmer by posing the data type of the expression of specific type is known as explicit type conversion. • The general form of a cast is : (type-name) expression • Here, ‘type-name’ is one of the standard C data types and the expression may be a constant, variable, or an expression. 8
  • 9.
    Use of ExplicitType Casting 9 Example Action X=(int)7.5 7.5 is converted to integer by truncation. a = (int)21.3/(int)4.5 Evaluated as (21/4) and the result would be 5. b = (double)sum/n Division is done in floating point mode. y = (int)(a+b) The result of (a+b) is converted to integer. z = (int)a+b a is converted to integer and then added to b. p = cos((double)x) Converts x to double before using it.
  • 10.
    Example of ExplicitType Conversion 10
  • 11.
    Example of ExplicitType Conversion 11
  • 12.
    Precedence & Associativity What is Precedence? • C has a precedence associated with it. This precedence is used to determine how an expression involving more than one operator is evaluated.  What is Associativity? • The operators at higher level of precedence are evaluated first. • The operators of the same precedence are evaluated either from ‘left to right’ or from ‘right to left’ depending on the level. This is known as the associativity property of an operator. 12
  • 13.
    Precedence & Associativity 13 Precedence rules decides the order in which different operators are applied.  Associativity rule decides the order in which multiple occurrences of the same level operator are applied.  C Operators with their Precedence & Associativity are listed in the following table.
  • 14.
    14 Operator Description AssociativityRank () [] Function call Array element reference Left to right 1 + - ++ -- ! ~ * & sizeof (type) Unary plus Unary minus Increment Decrement Logical Negation Ones complement Pointer reference Address Size of an object Type cast Right to left 2 * / % Multiplication Division Modulus Left to right 3 + - Addition Subtraction Left to right 4 << >> Left shift Right shift Left to right 5
  • 15.
    15 Operator Description AssociativityRank < <= > >= Less than Less than or equal to Greater than Greater than or equal to Left to right 6 == != Equality Inequality Left to right 7 & Bitwise AND Left to right 8 ^ Bitwise XOR Left to right 9 | Bitwise OR Left to right 10 && Logical AND Left to right 11 || Logical OR Left to right 12 ?: Conditional expression Right to left 13
  • 16.
    16 Operator Description AssociativityRank = *= /= %= += -= &= ^= |= <<= >>= Assignment operators Right to left 14 , Comma operator Left to right 15
  • 17.
    Summary of operatorprecedence 17  ‘Comma’ operator has lowest precedence.  ‘Unary Operators’ are operators having highest precedence.  Operators sharing common block in the above table have equal priority or precedence.  While solving expression, equal priority operators are handled on the basis of FIFO [First in First Out] i.e., Operator Coming First is handled First.
  • 18.
  • 19.
  • 20.
    20 Please give yourprecious feedbacks and suggestions at aakashsingh07041997@gmail.com