Skip to study material
← Question index

QUESTION 73 · Expressions and conversions

What is implicit type conversion? Give an example.

2 marks

Answer

Implicit type conversion is a conversion performed automatically by C without a written cast.

  • In double value = 5;, integer 5 is converted to double, giving 5.0.
  • A conversion can lose information: converting 3.8 to int discards the fractional part.

QUESTION 74 · Expressions and conversions

Explain implicit type conversion, integer promotions and mixed-type arithmetic.

5 marks

Answer in brief

Implicit type conversion is a conversion performed automatically according to C rules, without writing a cast at that point.

Where conversions occur

  1. Initialisation: In double value = 5;, integer 5 is converted to double.
  2. Assignment: In int count = 3.8;, the fractional part is discarded and count becomes 3.
  3. Mixed arithmetic: In 5 + 2.5, the integer is converted to double and the result is 7.5.
  4. Function arguments: Arguments can be converted to the parameter types. Extra promotion rules apply to functions such as printf.

Integer promotions

Small integer types such as char and short commonly become int before arithmetic. If int cannot represent all values of the original type, the promotion uses unsigned int. This allows many calculations on smaller integer values to use a standard arithmetic type.

Advantages

  1. Convenience: The programmer does not need to write a cast for every permitted conversion.
  2. Mixed calculations: Values of different arithmetic types can be used together according to the language rules.

Limitations and example

Automatic conversion can lose information. Converting 3.8 to int discards .8. Mixing signed and unsigned integers can also produce unexpected comparisons, so check the types. In double average = 7 / 2;, integer division happens first and gives 3; only then is 3 converted to 3.0. Use 7.0 / 2 or convert an operand before division to obtain 3.5.

Reference table

↔ Scroll horizontally on a small screen
Where conversions occur
ContextExampleEffect
Integer promotionA short used in arithmeticOften promotes to int; unsigned int is used when int cannot represent all values.
Mixed arithmeticint + doubleThe integer value is converted for double arithmetic.
Assignmentdouble result = 5;The integer value is converted to double.
Narrowing assignmentint count = 3.9;A representable floating value is truncated toward zero, producing 3.
Variadic function argumentsprintf("%f", a_float)float is promoted to double.

Key points

  • Implicit does not mean harmless or lossless.
  • Signedness matters in comparisons and arithmetic.

Related exam questions

Topic matches from the supplied JNTUH papers. Marks below are the printed allocations for those questions.

  • June 2022 · Q1(b) · 8 marks

    Explain different type-conversion techniques with examples.

    Paper P03 · page 1
  • January/February 2024 · Q1(b) · 1 marks

    What is the purpose of type conversion?

    Paper P08 · page 1
  • January/February 2024 · Q1(b) · 3 marks

    What is the need for type conversion? Explain.

    Paper P10 · page 1
View all matching exam references →