Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Types and Literals

Implementation-defined behavior in C

Concept page on implementation-defined behavior in C: sizes of types, char signedness, right shifts, struct layout and how to write code that does not depend on them.

The C standard leaves some choices to each compiler. When the choice is implementation-defined, the compiler must pick one behavior and document it, and the program stays valid. This is different from undefined behavior, where anything may happen, and from unspecified behavior, where the compiler may pick between several outcomes and need not say which (for example, the order in which function arguments are evaluated).

Portable code either avoids implementation-defined behavior or checks it. The examples below ask the compiler what it chose; their output describes the GCC 15 runner used on this site (a 64-bit Linux target), and another platform may print other numbers.

Try it

C Asking the implementation

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.

C Portable code that does not depend on the choices

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.

Frequently encountered choices

Area What the implementation decides Portable approach
Type sizes short, int, long, long long, pointers, wchar_t (only minimum ranges are guaranteed) <stdint.h> types, size_t for sizes
char signedness Plain char is signed or unsigned Use signed char or unsigned char for numbers; plain char for text
Right shift of negative numbers Arithmetic or logical shift Shift unsigned values
Out-of-range conversion to a signed type Wraps, saturates or raises a signal Range-check first
Struct layout Padding and alignment between members offsetof, sizeof; never write raw structs to files
Bit-fields Which bits are used first, whether int bit-fields are signed, whether fields may straddle units Masks and shifts on an unsigned integer
Integer-to-pointer casts Mapping of the value uintptr_t, and only for round trips
Character set Execution character set (UTF-8 on GCC/Linux) and the value of '\n' Avoid depending on numeric character codes
Floating point Rounding of some operations, FLT_EVAL_METHOD, extended precision Use float.h, compare with tolerances
Enum type Underlying integer type of an enum (C23 lets you fix it: enum e : unsigned char) Do not rely on sizeof(enum)
main forms and exit status Extra forms of main, meaning of exit codes other than 0, EXIT_SUCCESS, EXIT_FAILURE The standard forms
#pragma and #include "..." search paths Unknown pragmas, header lookup rules Wrapper headers and build systems

Usage notes

  • Your compiler documents every one of these. GCC lists its choices in the "Implementation" chapter of its manual; the C23 standard has an annex that collects them.
  • Check at compile time, so a wrong assumption stops the build: static_assert(sizeof(long) == 8, "needs 64-bit long"); or static_assert(CHAR_BIT == 8);.
  • The same program can behave differently on Windows (4-byte long, 2-byte wchar_t), Linux (8-byte long) and embedded systems (16-bit int).
  • Unspecified is not implementation-defined. The order of evaluating f() + g() is unspecified; the compiler need not document it and may vary it between two uses.
  • Locale-specific behavior (setlocale, strftime, toupper on non-ASCII, isalpha) depends on the environment, not only the compiler.
  • Extensions and warnings: compile with -std=c23 -pedantic to be told when you step outside the standard.