2.9 TYPE CONVERSION IN C

                                  

     Programming in C    



TYPE CONVERSION IN C

Though the C compiler performs automatic type conversions, a programmer must clearly understand how these conversions occur in order to correctly evaluate expressions and avoid loss of data or precision.

Type conversion in C refers to the process of converting one data type into another, either automatically by the compiler or explicitly by the programmer.


Type Conversion in C 

  1. Type Conversion in Expressions
  2. Conversion by Assignment 
  3. Casting Arithmetic Expressions 


1 Type Conversion in Expressions

When a C expression is evaluated, the result has a specific data type.

  • If all operands are of the same data type, the result is also of that type.

  • Example:
    If x and y are both of type int, then x + y is also of type int.

Expressions with Mixed Data Types

When operands are of different data types, the expression is evaluated to the largest data type present in the expression.

Data Type Hierarchy (Smallest → Largest)

char → int → long → float → double

Thus:

  • char + intint

  • long + floatfloat

  • float + doubledouble


Rules for Operand Promotion

Operands are promoted pairwise for every binary operator.




Promotion Rules

  • float operands are converted to double

  • char and short (signed or unsigned) are converted to int

  • If one operand is double, the other is converted to double

  • If one operand is long, the other is treated as long

  • If one operand is unsigned, the other is converted to unsigned

  • Otherwise, both operands are treated as int

📌 Note: Promotion does not change the original variable, only a temporary copy is used.


Example: Mixed Expression Type Promotion

char c; int j; float f; double d, r; r = (c*j) + (f/j) - (f+d);
  • c*j → promoted to int

  • f/jj promoted to float

  • f+df promoted to double

  • Final result → double

Hence, r is of type double.








2 Conversion by Assignment

Type conversion also occurs during assignment.

General Rule

The right-hand expression is converted to the type of the left-hand variable.

Promotion Example

float f; int i = 10; f = i; // int → float

Demotion Example

int i; float f = 1.23; i = f; // float → int
  • Fractional part is truncated

  • i becomes 1

  • f remains 1.23

⚠️ Important: Demotion may cause loss of data.


Conversions of Characters and Integers

There are six basic conversion methods:

1. Sign Extension

  • Used when converting signed smaller type to signed larger type

  • Preserves numerical value by extending sign bit

  • Example: short int → long int

2. Zero Extension

  • Used for unsigned to unsigned wider type

  • Extra bits filled with zeros

3. Preserve Low Order Data (Truncation)

  • Used when converting to a narrower type

  • Higher-order bits discarded

4. Preserve Bit Pattern

  • Used when converting between signed and unsigned types of same width

5. Internal Conversion

  • Uses hardware for integral ↔ floating point conversion

6. Truncate at Decimal Point

  • Used when converting floating point to integer

  • Fractional part is lost


Illustrative Program (Integer Conversions)

short int si; long int li; unsigned short int usi; unsigned long int uli; si = -10; li = si; // sign extension usi = 40000U; uli = usi; // zero extension uli = 0xabcdef12; usi = uli; // truncation si = usi; // preserve bit pattern

Output Highlights

  • Sign preserved during widening

  • Data loss during narrowing

  • Bit pattern preserved between signed & unsigned


Char Conversion Special Case

  • Behavior depends on whether char is signed or unsigned

  • ANSI C promotes char to signed int or unsigned int

  • Conversion may be implementation dependent


Conversions of Float and Double

  • All floating constants are double by default

  • Use suffixes:

    • f / Ffloat

    • l / Llong double

Examples:

  • 3.14 → double

  • 3.14f → float

  • 3.14L → long double

Overflow & Underflow

  • Results depend on system

  • May produce runtime errors, maximum values, or zero


Conversion Between Floating and Integral Types

  • Float → Int → fractional part truncated

  • Int → Float → value preserved (may lose precision for large numbers)

Example:

double x; int i; i = 1400; x = i; // int → double x = 14.999; i = x; // double → int (i = 14)



3 Casting Arithmetic Expressions

Casting is an explicit type conversion controlled by the programmer.

Syntax

(type) expression

Example:

(float)i

Need for Casting (Integer Division Problem)

int a = 100, b = 40; float c; c = a / b; // Result = 2.000000

Reason:

  • a/b is int division

  • Fractional part lost

Correct Code Using Casting

c = (float)a / b;

Now result = 2.5


Rounding a Floating Point Value

Rounding to Whole Number

N = A + 0.5;

Example:

n = 2.8 + 0.5; // n = 3

Rounding to Specific Decimal Precision

Rounding to 2 Decimal Places

R = (int)(F*100 + 0.5) / 100.0;

Example:

F = 2.468; R = (int)(F*100 + 0.5) / 100.0; // R = 2.47

⚠️ Use 100.0 to avoid integer division

Rounding to 3 Decimal Places

R = (int)(F*1000 + 0.5) / 1000.0;

Conclusion

Type conversion in C plays a critical role in:

  • Expression evaluation

  • Assignment operations

  • Precision control

  • Avoiding logical errors

Understanding automatic promotion, assignment conversion, and explicit casting is essential for writing correct and efficient C programs.






📖 Reference

The content for this subject is prepared by referring to the standard textbook:
“Computer Fundamentals and Programming in C”
Pradip Dey & Manas Ghosh
Second Edition, Oxford University Press (2018)

The explanations are exam-oriented and strictly aligned with the reference book.



Aivette-COI is created with the intention of helping college students learn smartly, revise quickly, and approach exams with confidence.

With love and care,
by Aivette 💙

Comments

Popular posts from this blog

22IMC7Z2 - CONSTITUTION OF INDIA

1.1 Historical Background of the Indian Constitution: The Company Rule & The Crown Rule

1.3 Preamble and Salient Features of the Indian Constitution