Reputation: 19
In this main function, I get an error message that I should not use %hu (format specifier for unsigned short), and that the type of unsigned short appears to be unsigned long. How could that be the case?
#include <stdio.h>
int main (void) {
unsigned short int a, b, c;
printf("sizeof short int - %hu bytes \n\n", sizeof(unsigned short int));
}
The shell output is:
warning: format specifies type
'unsigned short' but the argument has type
'unsigned long' [-Wformat]
...%hu bytes \n\n", sizeof(unsigned short int));
~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~
%lu
Upvotes: 0
Views: 198
Reputation: 75062
Result of sizeof
operator is size_t
regardless of what is passed to that.
Quote from N1570 6.5.3.4 The sizeof and _Alignof operators:
2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
5 The value of the result of both operators is implementation-defined, and its type (an unsigned integer type) is size_t, defined in <stddef.h> (and other headers).
The format specifier to print size_t
is %zu
.
Upvotes: 5