Skip to content
systemcoding.dev
Fundamentals

Unspecified vs Implementation-Defined vs Undefined Behaviour

Unspecified, implementation-defined and undefined behavior are three different promises in C. Learn what each guarantees and how to tell them apart.

7 min read
Summarize in AI
Flat illustration of two options weighed side by side, with the C, GNU and Rust logos.

The C standard has a family of phrases for "the language does not tell you exactly what happens here", and programmers treat them as synonyms. They are not. Unspecified behavior, implementation-defined behavior and undefined behavior make three different promises, and the difference decides whether your code is portable, merely platform-specific, or broken. This post defines each term against the wording of ISO/IEC 9899:2011 (C11), gives a working example of each, and ends with a way to classify any construct you meet. Examples assume GCC 14 and glibc on x86-64 Linux, with a note where AArch64 differs.

The three definitions

The terms are defined in section 3 of the standard, and the definitions are short enough to quote in paraphrase.

Implementation-defined behavior is "unspecified behavior where each implementation documents how the choice is made" (3.4.1). The standard gives a set of possible outcomes, or leaves the choice open, and the compiler vendor must write down which one they picked. Your code can rely on it, for that compiler.

Unspecified behavior is "use of an unspecified value, or other behavior where this International Standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance" (3.4.4). The set of outcomes is limited, but the implementation need not document the choice and may pick differently each time.

Undefined behavior is "behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements" (3.4.3). There is no set of outcomes.

The ordering is a ladder of how much you can rely on. Implementation-defined is the most predictable: one documented answer per compiler. Unspecified is next: a short list of valid answers, none documented. Undefined is last: no promise at all.

PropertyImplementation-definedUnspecifiedUndefined
Is the program valid?yesyesno meaning is assigned
Does the standard limit the outcomes?usually a lista listno
Must the compiler document it?yesnono
May it vary between runs?noyesyes
Can you rely on it?on one compileronly on the allowed setnever

Implementation-defined: documented choices

Anything that depends on the machine or the compiler's design, where a choice has to be made, falls here. Common examples:

  • The sizes and ranges of int, long and the other integer types. long is 8 bytes on 64-bit Linux and 4 bytes on 64-bit Windows.

  • Whether plain char is signed or unsigned.

  • The result of right-shifting a negative signed value (6.5.7p5).

  • The result of converting an out-of-range value to a signed integer type (6.3.1.3p3).

  • The representation of enumeration types, and the layout and order of bit-fields.

  • The format printed by %p.

GCC documents these in the "C Implementation-defined behavior" chapter of its manual. For example, it says that converting an out-of-range integer to a signed type takes the value modulo 2^N, and that right shifts of negative values are arithmetic shifts, which sign-extend.

impl-defined.cc
#include <stdio.h>
#include <limits.h>

int main(void) {
    char c = 200;
    printf("char is %s\n", CHAR_MIN < 0 ? "signed" : "unsigned");
    printf("c = %d\n", c);
    printf("-8 >> 1 = %d\n", -8 >> 1);
    printf("sizeof(long) = %zu\n", sizeof(long));
    return 0;
}

On x86-64 Linux with GCC, the output is:

Text
char is signed
c = -56
-8 >> 1 = -4
sizeof(long) = 8

char is signed on x86, so storing 200 gives -56 (200 minus 256), and -8 >> 1 is an arithmetic shift giving -4. On AArch64 Linux, GCC's default is unsigned char, so the same program prints char is unsigned and c = 200. Both outputs are conforming, and both are documented by the respective compiler. This is the definition of implementation-defined: the program is valid, the result is predictable on one platform, and it is not portable.

A portable program avoids relying on these, or states the assumption with _Static_assert(CHAR_MIN < 0, "needs signed char"), so the build fails on a platform where it would be wrong.

Unspecified: a choice that may change

Here the standard allows a few outcomes and the compiler does not have to say which. The classic examples all involve evaluation order:

  • The order in which function arguments are evaluated (6.5.2.2p10).

  • The order of evaluation of the operands of most binary operators, such as + and * (6.5p3).

  • Which of two subexpressions is evaluated first in f() + g().

  • The value of padding bytes in a struct, and the order in which qsort calls its comparison function.

order.cc
#include <stdio.h>

static int f(void) { puts("f called"); return 1; }
static int g(void) { puts("g called"); return 2; }

int main(void) {
    int sum = f() + g();
    printf("sum = %d\n", sum);
    return 0;
}

The result sum = 3 is certain. The order of the two lines before it is not. GCC and Clang can each print one order at one optimization level and the other at another, and both outputs are correct:

Text
f called
g called
sum = 3

That is one valid output, and the one with g called first is the other. The program is valid, since the standard says that either call may run first and both must run before the addition, and the result does not depend on the choice. It becomes a bug only when your logic depends on the order, such as f() setting a variable that g() reads.

Unspecified behavior turns into undefined behavior at one particular boundary: if the unspecified order lets two side effects hit the same object, the expression is undefined. a[i] = i++ is the textbook case. Each operand has a defined set of orders, but in none of them are the two accesses to i sequenced, so the whole expression has no meaning.

Undefined: no promise

Undefined behavior covers the constructs the standard declines to define at all. The signature feature is that it is not confined to the faulty operation. Signed integer overflow, a null dereference and an out-of-bounds access make the whole program execution undefined, and the compiler may assume none of them occurs.

undefined.cc
#include <limits.h>
#include <stdio.h>

int main(void) {
    int x = INT_MAX;
    printf("%d\n", x + 1);    // signed overflow
    return 0;
}

There is no list of outcomes and no documentation to read. With GCC and -O0 you will usually see -2147483648, and with other flags or versions the compiler may fold, reorder or remove the expression. Contrast that with the implementation-defined conversion in the previous example, where GCC documents the answer.

Some behaviors have a rule in the standard that makes them undefined, and a compiler can still document a stronger guarantee. GCC defines signed overflow as wrapping when you pass -fwrapv. That turns undefined into an implementation-defined extension for code built with that flag, and only for that compiler.

How to classify a construct

When you meet a surprising result, ask three questions in order.

  1. Does the standard say the program is invalid or give no meaning to it? If you can find the phrase "the behavior is undefined" in the relevant clause, or Annex J.2 lists it, stop. It is undefined, and no amount of testing on one machine proves it correct.

  2. Does the compiler's manual document the result? For GCC, the "C Implementation-defined behavior" chapter lists the choices. If yes, it is implementation-defined. It is portable only across implementations that agree, and _Static_assert can check the assumption.

  3. Is it a short list of valid outcomes with no documentation? Then it is unspecified. Make sure your code produces the same result for every allowed outcome.

The most common mistake is stopping after question 3 and assuming "the compiler picked an order, so I can rely on it". An unspecified choice may differ between optimization levels, compiler versions and even call sites in the same program.

A fourth category, locale-specific behavior, depends on the program's locale and is documented by the implementation. It matters for functions such as isalpha and strftime, and it behaves like implementation-defined behavior that varies at run time.

What compilers and tools tell you

GCC and Clang warn about some unspecified-order hazards with -Wsequence-point (included in -Wall), which flags expressions like i = i++ + 1 that are undefined. Implementation-defined assumptions are not diagnosed by default. You catch them by compiling with more than one compiler and target, by cross-compiling in CI for a big-endian or 32-bit target, and by encoding assumptions as _Static_assert checks at the top of the file. The undefined ones are the ones the sanitizers can surface at run time, with -fsanitize=undefined.

Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

How argc and argv Reach main in a C Program

The kernel copies argc and argv onto the new process's stack on execve, then startup code hands them to main. See the layout and measure it with a program.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.