The C standard guarantees that an int is able to store every possible array size. At least, that’s what I understand from reading §6.5.2.1, subsection 1 (Array subscripting constraints):
One of the expressions shall have type ‘‘pointer to object type’’, the other expression shall
have integer type, and the result has type ‘‘type’’.
Since we shall use ints as array subscripts, why are we supposed to use size_t to determine the size of an array?
Why does strlen() return size_t when int would suffice?
The term “integer type” doesn’t mean
int– for example,char, andshortare integer types.Just because you can use an
intto subscript an array doesn’t necessarily mean that it can reach all possible array elements.More specifically about
size_tvs.int, one example would be platforms whereintmight be a 16-bit type andsize_tmight be a 32-bit type (or the more common 32-bitintvs 64 bitsize_tdifference on today’s 64-bit platforms).