4

In 'absolute beginners guide to c' chapter 13

printf("The size of in is %d.\n", sizeof(int));

but clang throws...

warning: format specifies type 'int' but the argument has type
  'unsigned long' [-Wformat]

So I have to use format specifier %lu

Did the author get it wrong? What is the actual data type returned by sizeof()?

1

quoting the C99 standard (6.5.3.4 - 20):

C89, like K&R, defined the result of the sizeof operator to be a constant of an unsigned integer type. Common implementations, and common usage, have often assumed that the resulting type is int. Old code that depends on this behavior has never been portable to implementations that define the result to be a type other than int. The C89 Committee did not feel it was proper to change the language to protect incorrect code.

6.5.3.4 - 25:

The type of sizeof, whatever it is, is published (in the library header <stddef.h>) as size_t, since it is useful for the programmer to be able to refer to this type. This requirement implicitly restricts size_t to be a synonym for an existing unsigned integer type.

in summary, we can probably say that it's implementation-specific. that is, there were common implementations of the language that assumed it evaluated to a value of type int.

essentially it evaluates to size_t which is defined in stddef.h. in C99, the version that is currently being used in the course, it's restricted that size_t is defined to be a synonym of an existing unsigned integer type (e.g., unsigned long).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .