MCS-201 · Unit 2

Data Types, Operators and Expressions

Section 10 of 11

Block 1 · Unit 2Data Types, Operators and Expressions9 min · Section 10 of 11

2.10Type Conversion and the Cast Operator

Covers IGNOU MCS-201 §2.17

When an expression mixes types, such as an int and a double, C converts them to a common type before calculating. Most of the time this does what you expect, but some rules surprise beginners and cause logical errors. This section states the rules precisely and shows how to take control with a cast.

Automatic (implicit) conversion

An implicit conversion happens automatically. For binary arithmetic operators C applies the usual arithmetic conversions:

  1. Integer promotion: char and short operands are converted to int first.
  2. If either operand is long double, the other becomes long double.
  3. Otherwise, if either is double, the other becomes double.
  4. Otherwise, if either is float, the other becomes float.
  5. Otherwise both are integers: the operand of lower rank is converted to the higher-ranked type (int → long → long long), with special rules when one is unsigned.

Any floating type outranks every integer type, whatever their sizes. So in long_long_value + 1.0f, the 8-byte long long is converted to the 4-byte float.

Signed and unsigned mixing

When a signed int meets an unsigned int, the signed value is converted to unsigned. A negative number then becomes a very large positive one, so -1 < 1u is false (0). Compilers can warn about this with -Wextra (or -Wsign-compare). Avoid mixing signed and unsigned values in comparisons.

Conversion by assignment

In an assignment, the value is converted to the type of the variable on the left:

AssignmentStored valueNote
int i = 4.6;4Fraction discarded (demotion)
float b = 20;20.0Promotion
char c = 65;'A'Integer code stored as a character

The cast operator

A cast converts a value to a named type explicitly:

Syntax
(type) expression

A cast is a unary operator, so it applies to the operand immediately after it. (float)a / 5 converts a and then divides in floating point; (float)(a / 5) divides integers first and only then converts the truncated result.

Key takeaways

  • The usual arithmetic conversions convert both operands to a common type before an operation.
  • char and short are promoted to int; float stays float in ANSI C.
  • Assignment converts the result to the left-hand type; float to int truncates.
  • (type) expression casts the operand immediately following it.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

With int a = 9;, what is the value of (float)a / 2?

Show answer

Answer: B. 4.5

The cast converts a to 9.0 before dividing, so real division gives 4.5. Casting after the division would give 4.0.

Question 2 · multiple choice

With int a = 9;, what is the value of (float)(a / 2)?

Show answer

Answer: C. 4.0

a / 2 is integer division (4), and only that result is cast to 4.0. The fraction was already lost.

Question 3 · multiple choice

In char c = 'A'; int r = c + 1;, what happens to c before the addition?

Show answer

Answer: A. It is promoted to int

Integer promotion converts char and short operands to int first, so r is 66. Floating conversion happens only when a floating operand is involved.

Question 4 · multiple choice

What does this program print?

C · truncate.c
#include <stdio.h>

int main(void)
{
    int i = 7.9;
    float f = 7 / 2;
    printf("%d %.1f\n", i, f);
    return 0;
}
Show answer

Answer: C. 7 3.0

7.9 is truncated to 7 when stored in an int, and 7 / 2 is integer division (3), stored as 3.0. Rounding would give 8, but C truncates.

Question 5 · multiple choice

In standard C, what is the type of f * f when f is a float?

Show answer

Answer: D. float

ANSI C computes float * float as float; the old automatic float-to-double rule belongs to pre-standard C.

Question 6 · short answer

Why does if (-1 < 1u) behave as false? Explain and suggest a fix.

Show worked answer

1u is an unsigned int, so by the usual arithmetic conversions the int −1 is converted to unsigned int. −1 becomes the largest unsigned value (4,294,967,295 for 32 bits), and that is not less than 1, so the comparison gives 0. Fix: avoid mixing signed and unsigned operands, for example compare with 1 instead of 1u, or cast the unsigned value to a signed type when it is known to be small: -1 < (int)1u.

↑ ↓ move↵ openTab switch type