The C11 standard writes:
6.2.5 Types, ¶4:
There are five standard signed integer types, designated as
signed char
,short int
,int
,long int
, andlong long int
.
The five standard unsigned integer types would thus be unsigned char
, unsigned short int
, unsigned int
, unsigned long int
, and unsigned long long int
.
7.19 Common definitions <stddef.h>
, ¶1-2:
The header
<stddef.h>
[...] declares the following types. [...]size_t
which is the unsigned integer type of the result of thesizeof
operator
The presentation on cppreference.com
typedef /*implementation-defined*/ size_t;
implies that size_t
is always defined via a typedef
, and some answers to this question state/imply/assume that it is defined as one of the five standard unsigned integer types (see above) via a typedef
.
But couldn't size_t
also be defined as one of the specific-width integer types (C11 standard, 7.20 Integer types <stdint.h>
), such as uint32_t
? (Incidentally, what are they called? Wikipedia refers to them as "fixed-width integer types". The C11 standard occasionally uses the expression "extended integer type", though I can't find a prominent definition in 7.20 to that effect.) Must size_t
be defined using a typedef
as one of the five standard unsigned integer types? Or can it alternatively be defined as one of the specific-width integer types? Or can it be an entirely separate type? From what wording in the standard does the answer follow?
Edit: I found a relevant quote regarding this topic (Rationale for International Standard—Programming Languages—C, Revision 5.10 (April-2003), 6.5.3.4 The sizeof
operator):
The type of
sizeof
, whatever it is, is published (in the library header<stddef.h>
) assize_t
, since it is useful for the programmer to be able to refer to this type. This requirement implicitly restrictssize_t
to be a synonym for an existing unsigned integer type.
Edit: The integer types listed here on Wikipedia are called "specified-width integer types" in the C standard (I checked the C99 and C17 versions).
There is a C++ version of this question on Stack Overflow, but I can't deduce a clear answer for C.
Must size_t be defined using a typedef as one of the five standard unsigned integer types?
No, this is not required by the C standard.
… implies that
size_t
is always defined via atypedef
,…
This is inconsequential, as size_t
behaves in source code like a type, and how it came to be a type does not affect that. However, C 2018 7.19 1 says that <stddef.h>
declares “the following types”, of which size_t
is one. Defining it as a macro would not make it a type, so it must be declared with typedef
or some implementation extension that is equivalent.
… some answers to this question state/imply/assume that it is defined as one of the five standard unsigned integer types (see above) via a
typedef
.
Statements that do not cite an authority and are not written by an authority are not authoritative.
But couldn't size_t also be defined as one of the specific-width integer types (C11 standard, 7.20 Integer types <stdint.h>), such as uint32_t?
Yes. The specification of size_t
in C 2018 7.19 does not prohibit this.
Incidentally, what are they called? Wikipedia refers to them as "fixed-width integer types".
The title of C 2018 7.20.1.1 is “Exact-width integer types.”
The C11 standard occasionally uses the expression "extended integer type", though I can't find a prominent definition in 7.20 to that effect.
The term “extended integer types” is defined in C 2018 6.2.5 7, to be the extended signed integer types and the extended unsigned integer types. Those are defined in 6.2.5 4 and 6.2.5 6. They are merely additional integer types defined by the C implementation. A note notes they would have names in the portion of the name space reserved for identifiers in 7.1.3, notably an names beginning with underscore and an uppercase letter or another underscore. For example, __int48_t
could be such a name.
Must
size_t
be defined using atypedef
as one of the five standard unsigned integer types? Or can it alternatively be defined as one of the specific-width integer types? Or can it be an entirely separate type?
The wording in the standard is in C 2018 7.19 1:
The header
<stddef.h>
defines the following macros and declares the following types…
and 7.19 2:
…
size_t
which is the unsigned integer type of the result of the sizeof operator;…
That wording does not constrain size_t
to be one of the standard unsigned types nor to be one of the exact-width integer types, nor to be another type, nor does it prohibit it from being any of those types. (It does not even require that size_t
actually be able to represent all object sizes, so it is conceivable that specified result of a sizeof
operation may exceed what is representable in the size_t
type.)