• Home
  • 4.10 Integer Conversion During Expressions

4.10 Integer Conversion During Expressions

View Categories

4.10 Integer Conversion During Expressions

Integer conversion during expressions occurs when C converts integer operands to compatible types before performing an operation. These conversions affect arithmetic, comparisons, bitwise operations, and other expressions.

Two important rules are integer promotions and the usual arithmetic conversions. Integer promotions convert types such as char and short to int or unsigned int when required. The usual arithmetic conversions determine a common type for operands of different arithmetic types.

Example Program #

#include <stdio.h>

int main(void)
{
    int a = 7;
    int b = 2;

    double result1 = a / b;
    double result2 = (double)a / b;

    unsigned int x = 10;
    int y = -5;

    printf("Integer division = %.2f\n", result1);
    printf("Floating division = %.2f\n", result2);

    printf("Signed comparison: %d\n", y < 0);
    printf("Mixed comparison: %d\n", y < x);

    return 0;
}

Example Output #

On a typical system where int and unsigned int have the same width:

Integer division = 3.00
Floating division = 3.50
Signed comparison: 1
Mixed comparison: 0

Explanation #

1. Integer division

double result1 = a / b;

Both operands are int, so integer division occurs first. The result is 3, which is then converted to double and stored as 3.0.

2. Floating-point division

double result2 = (double)a / b;

The cast converts a to double. The other operand is also converted to double for the division, so the result is 3.5.

3. Comparing signed and unsigned integers

unsigned int x = 10;
int y = -5;

printf("%d\n", y < 0);
printf("%d\n", y < x);
  • y < 0 compares two signed integer values. Since y is -5, the expression is true and prints 1.
  • In y < x, the usual arithmetic conversions commonly convert y to unsigned int because int and unsigned int have the same rank and int cannot represent every value of unsigned int.
  • The converted value of y is a large unsigned value, so the comparison y < x is false and prints 0.

Common Integer Conversion Rules #

Situation Conversion behavior
char used in arithmetic Usually promoted to int
short used in arithmetic Usually promoted to int
int + int Operands remain int
int / int Integer division
int + unsigned int Commonly performed as unsigned int
int + double Integer operand converted to double
unsigned char + unsigned char Operands are typically promoted to int

The exact promotion of smaller integer types depends on whether int can represent all their values. The usual arithmetic conversions also depend on operand ranks and ranges.

Practical Example: Avoiding Integer Division #

Consider calculating an average:

int total = 17;
int count = 5;

double average = (double)total / count;

The result is 3.4. Without the cast, total / count would produce 3 through integer division, and assigning that result to double would yield 3.0.

Important Rules #

  • C performs conversions according to language rules before evaluating many operators.
  • Integer promotions and the usual arithmetic conversions are related but distinct processes.
  • Assigning an expression’s result to a wider type does not retroactively change how the expression was evaluated.
  • Mixing signed and unsigned integers can produce unexpected comparison results.
  • Use explicit casts when needed to make the intended arithmetic clear, but ensure the conversion itself is valid.

Key takeaway: Understand the types of the operands before evaluating an expression. This is essential for correct division, comparisons, and calculations involving signed, unsigned, and floating-point values.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *