Is `size_t` part of the standard library, or part of the C language itself?

Viewed 290

As a programmer, I can't use size_t unless I include stddef.h. This seems to imply this is a type defined in the C standard library. However, sizeof is a built-in language operator that returns a size_t. It seems weird to me that a built-in language feature would rely on the standard library. Is size_t "owned" by the library or the language?

1 Answers

Is size_t part of the standard library, or part of the C language itself?

The standard library, and the contents of the headers defined in clause 7 of the 2018 C standard, are part of the C language; everything specified in the C standard, including preprocessing, is part of the C language.

(In particular, the compiler is allowed to use knowledge of what routines in the standard library do even if the headers are not included. For example, if memcpy is used with file scope, the compiler is allowed to replace memcpy(&a, &b, sizeof *a) with a = b (assuming a and b are the same type) even if no standard library header is included.)

However, size_t is not a keyword reserved in 6.4.1, where the keywords are listed and is not specified in clause 6, which you might consider specifies a “core” part of the C language.

However, sizeof is a built-in language operator that returns a size_t.

sizeof returns a value with some type described as size_t in the text, but that identifier, size_t is not defined in the absence of including <stddef.h> or possibly other library header files. The type of sizeof is known to the compiler but does not necessarily have a name or may have some internal name known to the compiler, such as __size_t, or it may be a standard type, such as unsigned int.

Including stddef.h makes this type available to the programmer by defining the identifier size_t suitably.

Related