C Reference ยท Types and Literals
Type conversions in C
Concept page on C type conversions: integer promotions, the usual arithmetic conversions, signed/unsigned comparison traps, narrowing and float-to-int rules.
C converts operand types automatically in most expressions. The rules are simple but have sharp edges, especially where signed and unsigned integers meet. Understanding them explains bugs like -1 < 1u being false, or a char sum behaving as if it were an int.
Conversions happen in four situations: integer promotions on small types, the usual arithmetic conversions for binary operators, conversion on assignment or argument passing to the target type, and explicit casts. This page covers the implicit ones.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
The rules in order
1. Integer promotions. Any integer type with a rank lower than int (_Bool, char, short, bit-fields) is converted to int if int can represent all its values, otherwise to unsigned int. Arguments to variadic functions such as printf also get promotions, and float becomes double.
2. Usual arithmetic conversions (both operands of + - * / % < > == & | ^ ?:):
- If either operand is
long double, the other becomeslong double; else if either isdouble,double; else if either isfloat,float. - Otherwise both are promoted, then: if the types match, done.
- If both are signed or both unsigned, the narrower converts to the wider.
- If the unsigned operand has rank at least that of the signed, the signed one converts to unsigned.
- Otherwise, if the signed type can represent every value of the unsigned one, convert to the signed type; else to the unsigned version of the signed type.
3. Conversion on assignment, return and function arguments goes to the declared type. The value is converted, and information may be lost.
Value changes
| From โ to | Result |
|---|---|
| Integer โ wider integer | Value preserved. |
| Any integer โ unsigned | Reduced modulo 2^N; defined. |
| Integer โ signed, value does not fit | Implementation-defined result or signal (GCC wraps). |
| Floating โ integer | Fractional part discarded; undefined behavior if the result does not fit. |
| Integer โ floating | Exact if representable, otherwise rounded. |
| Pointer โ integer | Implementation-defined; use uintptr_t. |
Any scalar โ bool |
0 stays 0, anything else is 1. |
Usage notes
- Never mix signed and unsigned in comparisons.
-1 < 1uis false because-1becomesUINT_MAX. GCC's-Wsign-compare(in-Wextra) flags it. - Division and modulo use the promoted type.
double r = a / b;divides integers first. Cast one operand before the operator. chararithmetic isintarithmetic, soc + 1is anint. Assign back tocharto narrow.- A float-to-int out-of-range conversion is undefined behavior, not saturating.
- Use
-Wconversionto find silent narrowing, and checked helpers likeckd_addfor overflow. - Constants matter:
1 << 31overflowsint; use1u << 31or1ULL.