I encountered this unexpected output with the following code in which I was verifying the maximum values (represented in decimal form) of the unsigned forms of short and int
Using printf()
, your conversions in the format string must match the type of the arguments, otherwise the behavior is undefined. %d
is for int
.
Try this for the maximum values:
#include <stdio.h>
int main()
{
printf("Max unsigned int = %u\n", (unsigned)-1);
printf("Max unsigned short = %hu\n", (unsigned short)-1);
return 0;
}
Side notes:
-1
cast to that type.stdout
s buffer with the default setting of line buffered.You need to use %u
for the format specifier for an unsigned
type.