Section 4.13

Implementation-defined behavior

Definition

Implementation-defined behavior is the C standard’s name for behavior that the standard leaves open among stated possibilities, and that each implementation must choose and document. An implementation is one compiler, with its options and libraries, for one target processor. Examples are whether the type char is signed, the number of bits in an int, and the result of shifting a negative number right. A program that relies on such a choice is correct only where the choice falls that way. Unspecified behavior §4.14 needs no documentation; undefined behavior §4.12 has no requirements.

The idea

A program reads one byte of a network packet into a variable of type char and uses it as a length. The byte is 0xC8, 200 in decimal. Compiled for a desktop computer with an x86-64 processor, char is signed, so the variable holds −56-56. Compiled for a 32-bit Arm microcontroller §6.1, char is unsigned, so the same variable holds 200. Neither compiler is wrong: the C standard lets each implementation choose, provided it writes the choice down. The program is wrong if it assumes one of the two choices without saying so and without checking.

C leaves such choices open so that each implementation can use what is natural and fast on its processor, such as the processor’s register width for int. The price is that the same source code can mean different things on different machines. The documentation requirement makes the meaning knowable. It does not make the meaning the same everywhere.

How it works

C11, the 2011 edition of the C standard, requires every implementation to come with a document that defines all of its implementation-defined characteristics (clause 4, paragraph 8), and lists those characteristics in its Annex J.3. The manual of the C compiler GCC §12.17 (GNU Compiler Collection; GNU stands for GNU’s Not Unix) has a chapter on them. The ABI §12.47 (application binary interface), the document that fixes how compiled code for one processor family lays out data and calls functions, settles many of them for every compiler of that platform.

The choices that matter most:

Some neighboring cases look similar but are undefined behavior, which no documentation makes safe: a shift by a negative count or by at least the width of the type, a left shift of a negative value, and a left shift of a signed value whose result does not fit (C11 clause 6.5.7). Processors disagree on these. For a 32-bit shift, x86 uses only the lowest 5 bits of the count, so a shift by 32 shifts by 0; 32-bit Arm uses the lowest 8 bits, so a shift by 32 gives 0.

An assumption about an implementation-defined choice is handled in one of three ways, in this order of preference.

  1. Remove it. Write unsigned char or uint8_t for bytes, never plain char. Use the exact-width types of the header stdint.h, such as int32_t, when a width matters: C11 requires that int32_t, if it exists, has exactly 32 bits, no padding bits and two’s complement encoding. Shift unsigned values only.

  2. Check it when the program is built. A static assertion, written _Static_assert in C11, stops the compilation when a constant condition is false, for example _Static_assert(CHAR_BIT == 8, "8-bit bytes") or _Static_assert((-1 >> 1) == -1, "arithmetic shift"). A build for an implementation that chose differently then fails, instead of producing a wrong program.

  3. Document it, next to the code that depends on it, together with the compiler, the options and the ABI for which it was checked.

Worked example

A packet carries a 1-byte length, and a function copies that many bytes into a 64-byte buffer buf. The packet’s bytes are in an array §14.5 pkt of type char:

char len = pkt[0];
if (len > 64) return -1;
memcpy(buf, pkt + 1, len);

The packet’s first byte is 0xC8, the bit pattern 11001000.

Declaring len as uint8_t gives 200 on both machines, and the test then rejects the packet everywhere.

Take int x = -20, with 32 bits in two’s complement: the pattern is 0xFFFFFFEC. Under GCC’s arithmetic shift, x >> 2 copies the sign bit into the two vacated top positions and gives 0xFFFFFFFB, which is −5-5. A logical shift would give 0x3FFFFFFB, which is 1,073,741,819. Even the arithmetic shift is not division. For x=−21x = -21, x >> 2 gives −6-6, which is −21/4=−5.25-21 / 4 = -5.25 rounded down, while the C expression x / 4 gives −5-5, because C division rounds toward zero.

A device counts milliseconds since start-up in a variable of type long. The largest 32-bit signed value is 231−1=2,147,483,6472^{31} - 1 = 2{,}147{,}483{,}647, and one day has 1000×86,400=86,400,0001000 \times 86{,}400 = 86{,}400{,}000 milliseconds, so a 32-bit counter reaches its largest value after 2,147,483,647/86,400,000≈24.862{,}147{,}483{,}647 / 86{,}400{,}000 \approx 24.86 days. Where long has 32 bits, as on 32-bit Arm, the counter overflows after about 24.86 days, and because the type is signed, that overflow is undefined behavior. Where long has 64 bits, as on 64-bit Linux, the counter lasts about 292 million years. Declaring the counter int64_t makes the width the same everywhere.

Stated precisely

Clause 3.4.1 of C11 defines implementation-defined behavior as “unspecified behavior where each implementation documents how the choice is made”, and gives as its example “the propagation of the high-order bit when a signed integer is shifted right”. Unspecified behavior (clause 3.4.4) is behavior for which the standard provides two or more possibilities and imposes no further requirement on which is chosen. Implementation-defined behavior is therefore the documented kind of unspecified behavior.

The clauses for the cases above are these. Plain char has the same range, representation and behavior as either signed char or unsigned char (clause 6.2.5, paragraph 15). For E1 >> E2 with E1 of a signed type and a negative value, the resulting value is implementation-defined (clause 6.5.7, paragraph 5). Converting a value to a signed integer type that cannot represent it gives an implementation-defined result or raises an implementation-defined signal (clause 6.3.1.3, paragraph 3). The minimum ranges of the integer types are in clause 5.2.4.2.1.

A program is portable with respect to an implementation-defined characteristic when it is correct for every choice the standard allows. Otherwise it is correct only on the implementations whose documented choice it assumes, and that set of implementations must be stated. For each characteristic the program relies on, record which of three things fixes it: the standard (for example, int has at least 16 bits), the implementation’s documentation and ABI (for example, int has exactly 32 bits), or a check in the program itself, such as a static assertion.

Limits and common mistakes

Documented is not the same as portable. A documented choice is reliable for that implementation only. Code that depends on it breaks without warning when it is built with another compiler, for another processor, or with other options.

The options are part of the implementation. The option -funsigned-char changes the meaning of every plain char in the program. Record the compiler, its version and its options together with each assumption.

Implementation-defined is not undefined. An implementation-defined operation always gives a documented result, and the compiler may not assume that it never happens. An undefined operation, such as shifting a 32-bit value by 32, has no result to document, and the compiler may assume that it never happens.

Exact-width types fix the width, not the arithmetic. Two uint8_t values are converted to int before they are added, under the rules that the Integer promotion and conversion §4.18 entry explains.

Data layout is implementation-defined too. The order of the bytes of a multi-byte value (its endianness §4.21), the padding between the members of a structure and the layout of bit-fields are left to the implementation, and the ABI fixes them for a platform. They matter whenever data leaves the program as bytes, in files or messages, which the Serialization §4.28 entry treats.

Questions answered

Which assumptions about shifts, char signedness, and integer widths need documentation?

Every assumption about a choice that the C standard leaves to the implementation needs documentation, and for these three topics the choices are: whether right-shifting a negative signed value copies the sign bit (an arithmetic shift) or fills with zeros (a logical shift); whether plain char, written without signed or unsigned, is signed or unsigned; and the exact widths and ranges of int, long and the other integer types, together with the number of bits in a byte and the encoding of negative numbers. The standard calls these implementation-defined: each compiler and target must choose one of the allowed behaviors and write the choice down.

Neighboring assumptions that the standard leaves undefined cannot be made safe by documentation: a shift by a negative count or by at least the width of the type, and a left shift of a negative value. They must be avoided. The best practice is to remove each dependence where possible (unsigned char or uint8_t for bytes, exact-width types such as int32_t, shifts of unsigned values only), to turn each remaining one into a static assertion that stops the build on an implementation that chose differently, and to document the compiler, its options and the ABI (application binary interface) for which the rest was checked.

Related entries: Unspecified behavior (§4.14), Integer promotion and conversion (§4.18), Shift semantics (§4.20), Undefined behavior (UB) (§4.12), Endianness (§4.21). Further reading: [78, 309].