I am attempting to do exercise 2-1 of K&R C but when I run the program, the result is this:
UNSIGNED TYPES
UNSIGNED CHAR: 0 255
UNSIGNED SHORT: 0 65535
UNSIGNED INT: 0 -1
UNSIGNED LONG; 0 -1
The result of UNSIGNED INT
and UNSIGNED LONG
weren't supposed to be -1
, it was supposed to be the value in the reference. (Appendix B.11 Page 213)
/* UNSIGNED TYPES */
printf("\n\tUNSIGNED TYPES\n");
printf("UNSIGNED CHAR: %d %d\n", 0, UCHAR_MAX);
printf("UNSIGNED SHORT: %d %d\n", 0, USHRT_MAX);
printf("UNSIGNED INT: %d %d\n", 0, UINT_MAX);
printf("UNSIGNED LONG: %d %d\n", 0, ULONG_MAX);
The strange part is that I don't think there's something wrong with my code, the minimum range is 0
because I read that unsigned
types are non-negative. I already included limits.h
and float.h
.
How do I solve this problem?
The link of the full program is here.
UPDATE 1: I tried replacing %d
of the max range for unsigned long
and that seemed to work but the unsigned int
is 4294967925
instead of 65535
. Thanks for helping me fix the unsigned long
problem and I hope you guys can help me with the unsigned int
issue.
UPDATE 2: I changed the ULONG_MAX
's character sequence from %ul
to %lu
.
Here's the code and the result:
Code:
/* UNSIGNED TYPES */
printf("\n\tUNSIGNED TYPES\n");
printf("UNSIGNED CHAR: %d %u\n", 0, UCHAR_MAX);
printf("UNSIGNED SHORT: %d %u\n", 0, USHRT_MAX);
printf("UNSIGNED INT: %d %u\n", 0, UINT_MAX);
printf("UNSIGNED LONG: %d %lu\n", 0, ULONG_MAX);
Result:
UNSIGNED TYPES
UNSIGNED CHAR: 0 255
UNSIGNED SHORT: 0 65535
UNSIGNED INT: 0 4294967925
UNSIGNED LONG; 0 4294967925