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
- Type Conversion in Expressions
- Conversion by Assignment
- 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:
Ifxandyare both of type int, thenx + yis 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 + int→ int -
long + float→ float -
float + double→ double
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
-
c*j→ promoted to int -
f/j→jpromoted to float -
f+d→fpromoted 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
Demotion Example
-
Fractional part is truncated
-
ibecomes 1 -
fremains 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)
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
charto 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 / F→ float -
l / L→ long 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:
3 Casting Arithmetic Expressions
Casting is an explicit type conversion controlled by the programmer.
Syntax
Example:
Need for Casting (Integer Division Problem)
Reason:
-
a/bis int division -
Fractional part lost
Correct Code Using Casting
Now result = 2.5
Rounding a Floating Point Value
Rounding to Whole Number
Example:
Rounding to Specific Decimal Precision
Rounding to 2 Decimal Places
Example:
⚠️ Use 100.0 to avoid integer division
Rounding to 3 Decimal Places
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
Post a Comment