• Home
  • 4.6 Type Demotion

4.6 Type Demotion

View Categories

4.6 Type Demotion

Type demotion occurs when a value is converted from a type with a wider range or greater precision to a type with a narrower range or lower precision. This commonly happens when converting double to float, long to int, or int to char.

Type demotion can result in loss of precision, loss of range, or a change in the represented value. The exact behavior depends on the source and destination types.

Example Program #

#include <stdio.h>

int main(void)
{
    double precise_value = 123.456789;
    float reduced_value = (float)precise_value;

    int number = 300;
    unsigned char small_number = (unsigned char)number;

    printf("Original double = %.9f\n", precise_value);
    printf("Converted float = %.9f\n", reduced_value);

    printf("Original integer = %d\n", number);
    printf("Converted unsigned char = %u\n",
           (unsigned int)small_number);

    return 0;
}

Example Output #

On a common system where float has less precision than double and unsigned char is an 8-bit type:

Original double = 123.456789000
Converted float = 123.456787109
Original integer = 300
Converted unsigned char = 44

Explanation #

1. Demotion from double to float

double precise_value = 123.456789;
float reduced_value = (float)precise_value;
  • double typically provides more precision than float.
  • Converting the value to float may lose precision.
  • The converted value is the nearest representable float value when the conversion is supported and rounds accordingly. The exact result depends on the implementation.

2. Converting an integer to a smaller unsigned type

int number = 300;
unsigned char small_number = (unsigned char)number;
  • unsigned char can represent values from zero through UCHAR_MAX.
  • On a system where unsigned char ranges from 0 to 255, converting 300 produces 44.
  • This happens because conversion to an unsigned integer type is performed modulo one more than the maximum value of that type.

The exact result depends on the range of unsigned char on the implementation.

Common Type Demotion Examples #

Conversion Possible effect
double → float Loss of precision or range
long → int Value may change if it is outside the destination range
int → char Result depends on the range and signedness of char
int → unsigned char Value is reduced modulo UCHAR_MAX + 1
long double → double Loss of precision or range

Important Rules #

  • Type demotion does not always change the value. If the destination type can represent the value exactly, the value may be preserved.
  • Converting a floating-point value to a narrower floating-point type may change its precision or range.
  • When converting an integer to a signed integer type that cannot represent the original value, the result is implementation-defined or an implementation-defined signal may be raised, depending on the applicable C standard.
  • Converting an integer to an unsigned type follows the modulo conversion rule.
  • Explicit casts make conversions visible in the code, but they do not prevent data loss.

Key takeaway: Type demotion is important whenever a value moves into a type with a narrower range or lower precision. Check the destination type’s limits before converting, especially when handling large integers or precise floating-point values.

Powered by BetterDocs

Leave a Reply

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