Implementation-Defined Behavior
Implementation-defined behavior is behavior the C standard deliberately leaves to each compiler and platform, with the requirement that the choice is documented. The program stays well defined; the answer varies between targets.
Detailed Explanation
C runs on 8-bit microcontrollers, 64-bit servers, and hardware nobody has built yet. A standard that pinned down the width of int or the layout of a struct would either exclude some machine or force every machine to pay for emulation. So the committee split the difference: for a set of choices, the standard says "pick one and write it down".
That second half matters and is easy to skip. An implementation must document its choices, usually in a "C implementation-defined behavior" appendix of the compiler manual. Annex J.3 of the standard is the index of everything that falls into this category, and it runs to several pages.
The ones you will actually meet:
- Whether plain
charis signed or unsigned. Signed on x86 Linux, unsigned on ARM Linux and on most ARM-based phones.signed charandunsigned charare separate types from plainchar, always. - The width of the integer types. The standard sets only minimum ranges:
intis at least 16 bits,longat least 32,long longat least 64.intis 32 bits on nearly every modern platform, but "nearly every" is not "every". CHAR_BIT, the number of bits in a byte. At least 8. Exactly 8 on anything you are likely to target; some DSPs use 16 or 32.- Right shift of a negative signed value.
-8 >> 1may be an arithmetic shift giving-4, or a logical shift giving a large positive number. Mainstream compilers all use arithmetic shift and document it. - Struct padding. How many bytes the compiler inserts between members to satisfy alignment, and therefore
sizeof(struct ...), is implementation-defined. - Converting a pointer to an integer and back. Legal, and defined by the implementation, provided the integer type is wide enough.
uintptr_tfrom<stdint.h>exists for exactly this and is optional but universally available in practice. - Whether a bit-field of plain
inttype is signed, and how bit-fields are packed.
Two contrasts keep this straight:
| Category | Standard's promise | Documented? |
|---|---|---|
| Implementation-defined | one of several listed behaviors | yes, by the compiler |
| Unspecified | one of several listed behaviors | no |
| Undefined | nothing at all | no |
Unspecified behavior includes things like the order in which function arguments are evaluated, the values of padding bytes in a struct, and whether two identical string literals share storage. The program is still valid; you cannot rely on which choice you get, and the compiler need not be consistent from one occurrence to the next.
One correction to a common belief: the sign of % with negative operands is not implementation-defined in modern C. C89 left it open, but C99 fixed division to truncate toward zero, so -7 / 2 is -3 and -7 % 2 is -1 on every conforming compiler. Old books still say otherwise.
What does this cost you? Portability work, mostly at boundaries. Use the fixed-width types from <stdint.h> when a value crosses a wire, a file, or an ABI. Use unsigned char when you mean a raw byte. Use size_t for sizes. Divide instead of shifting when the value can be negative and you want the arithmetic meaning. Inside a single program on a single target, implementation-defined behavior is harmless, which is exactly why it is so easy to write code that only works on your laptop.
Code Examples
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
struct packet {
char kind; // 1 byte
int length; // usually forced to a 4-byte boundary
char flag; // 1 byte, then trailing padding
};
int main(void) {
// %zu is the correct conversion specifier for size_t.
printf("CHAR_BIT = %d\n", CHAR_BIT); // 8 on mainstream targets, >= 8 guaranteed
printf("sizeof(int) = %zu\n", sizeof(int)); // 4 on mainstream targets, >= 2 guaranteed
printf("plain char is %s\n", CHAR_MIN < 0 ? "signed" : "unsigned");
printf("-8 >> 1 = %d\n", -8 >> 1); // -4 with an arithmetic shift
printf("sizeof(packet) = %zu\n", sizeof(struct packet)); // often 12, not 6
// Pointer-to-integer round trip. uintptr_t is the type designed for it.
int x = 0;
uintptr_t as_int = (uintptr_t)&x;
int *back = (int *)as_int;
printf("round trip ok = %d\n", back == &x); // 1
return 0;
}#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
// Portable byte handling. Three habits remove most surprises.
// 1. Use unsigned char when the value is a raw byte, never plain char.
static unsigned checksum(const unsigned char *bytes, size_t n) {
unsigned sum = 0;
for (size_t i = 0; i < n; i++) {
sum += bytes[i]; // no sign extension, because the type is unsigned
}
return sum;
}
// 2. Convert to unsigned char before calling a <ctype.h> function.
// Passing a negative plain char other than EOF is undefined behavior.
static int is_space(char c) {
return isspace((unsigned char)c);
}
// 3. Divide instead of shifting when the value may be negative and you
// want the arithmetic meaning rather than the bit pattern.
static int halve(int v) {
return v / 2; // -7 / 2 is -3 on every C99 compiler
}
int main(void) {
const unsigned char data[] = {0x80, 0x7f, 0x01};
printf("%u %d %d\n", checksum(data, sizeof data), is_space(' '), halve(-7));
// 256 1 -3
return 0;
}Common Gotchas
Passing a plain `char` to `<ctype.h>` can be undefined
on platforms where `char` is signed, a byte such as `0xE9` becomes a negative `int`. `isalpha`, `toupper`, and friends accept only values representable as `unsigned char` or `EOF`. Always cast: `isalpha((unsigned char)c)`.
Left shift is not the mirror of right shift
right-shifting a negative value is implementation-defined, but left-shifting a negative value, or shifting by a count at least as wide as the type, is undefined behavior. The two sit in different categories.
`sizeof(struct)` is not the sum of its members
padding is implementation-defined, so never compute a struct's on-disk size by adding member sizes, and never `memcmp` two structs to compare them. Padding bytes hold unspecified values.
Implementation-defined code is still correct code
relying on 32-bit `int` inside a program that only ever ships for x86-64 is a documented, deliberate choice. Record it, and put a `_Static_assert` on the assumption so the build fails loudly on a new target.