SoFunction
Updated on 2025-04-13

Detailed explanation of type conversion rules for integer and floating point operations in C language

Type conversion rules for integer and floating point operations in C language

In C, different data types will be performed during operation.Implicit type conversion

whenSigned integers (intUnsigned integers (unsigned intandFloating point type (floatdoubleWhen performing calculations,The compiler will automatically adjust the data type of the operation according to the type priority and conversion rules.

1. Int and unsigned int conversion rules when participating in operation

whenintandunsigned intPerform operations, the calculation result is usually converted tounsigned int, the reasons are as follows:

(1) Unsigned Dominance Rule

C language regulations:

If an operand isint, another operand isunsigned int, and they have the same width (like 32 bits), thenint Will be promoted tounsigned int

(2) Example

#include <>

int main() {
    int a = -5;
    unsigned int b = 10;

    if (a < b) {  // a will be converted to unsigned int        printf("a < b is true\n");
    } else {
        printf("a < b is false\n");
    }
    return 0;
}

Output:

a < b is false

analyze:

  • ayes-5int),byes10unsigned int)。
  • aIt will be converted tounsigned int-5Become4294967291(Under 32-bit system).
  • 4294967291 > 10,soa < bbecomefalse, contrary to intuition.

(3) Methods to avoid problems

  1. Use explicit type conversion
if ((int)a < (int)b) {
    printf("Correct comparison\n");
}
  1. avoidint andunsigned intMixed
unsigned int a = 5;
unsigned int b = 10;
  1. usesize_tMake a safe comparison
    • size_tIt is an unsigned integer, suitable for array indexing and other situations.

2. Unsigned int and floating point numbers (float / double) conversion rules when participating in operations

whenunsigned intandfloat / double Perform operations, the calculation result is usually converted toFloating point type (float / double, the reasons are as follows:

(1) Floating point type is preferred

C language regulations:

If an operand isunsigned int, another operand isfloat ordouble,butunsigned intIt will automatically convert tofloat / double

(2) Why not convert to unsigned int?

  1. The representation range ratio of floating point numbersunsigned intBigger

    • 32 bitsunsigned intThe maximum value is42949672952^32 - 1)。
    • floatCan be expressed~3.4 × 10^38doubleCan be expressed~1.8 × 10^308
    • The range of floating-point numbers can be represented far exceeds that of unsigned integers, so the conversion direction isunsigned int → float/double, without reverse conversion.
  2. Floating points can represent decimals, but integers cannot

    • For example:5 / 2.0 = 2.5, if converted tounsigned int, will be lost.5,become2, which results in a loss of accuracy.
    • To avoid accuracy loss, C language will default tounsigned intConvert tofloat ordouble Perform calculations.

(3) Example

#include &lt;&gt;

int main() {
    unsigned int a = 10;
    float b = 3.5;

    float result = a + b;  // `a` convert to `float`    printf("Result: %f\n", result); 

    return 0;
}

Output:

Result: 13.500000

analyze:

  • aunsigned int) is converted tofloat, becomes10.0f
  • calculate10.0f + 3.5f = 13.5f, the result type isfloat

3. Summary of type conversion rules

Operation type Result Data Type illustrate
int + unsigned int unsigned int int is converted to unsigned int first, which may cause negative numbers to become larger
unsigned int + float float unsigned int is converted to float first, and then calculated
unsigned int + double double unsigned int is converted to double first, and then calculated
unsigned int * float float unsigned int is converted to float first, and then calculated
unsigned int * double double unsigned int is converted to double first, and then calculated

Key summary

  1. int andunsigned intWhen computing,int Will be converted tounsigned int

    • A negative number may become a very large positive number, resulting in a logical error.
  2. unsigned intandfloat/doubleWhen computing,unsigned intWill be converted tofloat/double

    • Avoid accuracy loss and ensure the accuracy of floating-point calculations.
  3. To avoid type conversion problems, it is recommended

    • Unify variable types to avoidintandunsigned intMixed use.
    • Use explicitlyfloatordoubleDo floating point calculations and do not rely on implicit conversions.
    • usesize_tProcess array indexes to avoidunsigned intProblems brought about.

These rules apply to C, and also to C++ and other similar programming languages.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.