0% found this document useful (0 votes)
6 views17 pages

C ProgType Conversion, Precedence and Associativity

The document explains type conversion in programming, detailing implicit and explicit type conversions. Implicit conversion occurs automatically when different data types are involved, while explicit conversion requires the programmer to specify the type. Additionally, it covers operator precedence and associativity in C, which determine the order of operations in expressions.

Uploaded by

rpandeybe24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views17 pages

C ProgType Conversion, Precedence and Associativity

The document explains type conversion in programming, detailing implicit and explicit type conversions. Implicit conversion occurs automatically when different data types are involved, while explicit conversion requires the programmer to specify the type. Additionally, it covers operator precedence and associativity in C, which determine the order of operations in expressions.

Uploaded by

rpandeybe24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Type Conversion

Precedence & Associativity


Type Conversion

• What is Type Conversion?


or Type Conversion is a way to convert a
– Type Casting
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

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 long double


can be used double
while evaluating float
expressions.
Unsigned long int
long int Conversion
Hierarchy
Unsigned int
int
short char
5
Example of Implicit Type Conversion

6
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.

7
Use of Explicit Type Casting

Example Action
7.5 is converted to integer by
X=(int)7.5
truncation.
Evaluated as (21/4) and the result
a = (int)21.3/(int)4.5
would be 5.

b = (double)sum/n Division is done in floating point mode.

The result of (a+b) is converted to


y = (int)(a+b)
integer.
a is converted to integer and then
z = (int)a+b
added to b.

p = cos((double)x) Converts x to double before using it.

8
Example of Explicit Type Conversion

9
Example of Explicit Type Conversion

10
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.

11
Precedence & Associativity

• 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.

12
Operator Description Associativity Rank

() Function call Left to right 1


[] Array element reference

+ Unary plus Right to left 2


- Unary minus
++ Increment
-- Decrement
! Logical Negation
~ Ones complement
* Pointer reference
& Address
sizeof Size of an object
(type) Type cast
* Multiplication Left to right 3
/ Division
% Modulus
+ Addition Left to right 4
- Subtraction
<< Left shift Left to right 5
>> Right shift 13
Operator Description Associativity Rank

< Less than Left to right 6


<= Less than or equal to
> Greater than
>= Greater than or equal to

== Equality Left to right 7


!= Inequality

& 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

14
Operator Description Associativity Rank
= Assignment operators Right to left 14
*= /= %=
+= -= &=
^= |=
<<= >>=

, Comma operator Left to right 15

15
Precedence
Example

16
Example of Associativity

Associativity
Example
17

You might also like