Should I include header file with definition of size_t (e.g. cstddef or stddef.h) for using sizeof keyword, which returns that type?
For example:
//..no include
int main()
{
size_t n; //error: unknown type size_t
sizeof(int); //ok, but returned size_t type also undefined
return 0;
}
You only need to include
cstddef
orstddef.h
if you need to explicitly usesize_t
(i.e. in a declaration or definition). It is not necessary if if all you want to do it determine the size of a specific type usingsizeof
. For example the following statements do not require that you include any header file to usesizeof
in determine the size of a type.If you're looking specifically for information about
sizeof
I suggest you take a look at 5.3.3 ([expr.sizeof]
) of the C++ Standard for more information.