problem with a simple program in C
From C++ to PHP, debugging to webhosting; help and discussion about writing your latest program to running your website. NOT for help when your PC won't work.
| Announcements | Posted on | |
|---|---|---|
| Enter our travel-writing competition for the chance to win a Nikon 1 J3 camera | 21-05-2013 | |
-
problem with a simple program in C
here my code:
Code:#include <stdio.h> #include <limits.h> #include <float.h> int main() { //chars int maxUChar, maxSChar, minSChar; maxSChar = SCHAR_MAX; minSChar = SCHAR_MIN; maxUChar = UCHAR_MAX; printf("The range of a schar is: %d <= x <= %d\n", minSChar, maxSChar); printf("The range of a uchar is: 0 <= x <= %d\n", maxUChar); printf("\n"); //ints int maxUInt, maxSInt, minSInt; maxUInt = UINT_MAX; maxSInt = INT_MAX; minSInt = INT_MIN; printf("The range of an int is: %d <= x <= %d\n", minSInt, maxSInt); printf("The range of a uint is: 0 <= x <= %d\n", maxUInt); }
here's my output:
the last line (that reads "printf("The range of a uint is: 0 <= x <= %d\n", maxUInt);") appears to print out "The range of uint is: 0 <= x <= -1".
Why does it say -1?
According to the O'Reilly C Programming Language book it should say 65536 instead of -1
-
Re: problem with a simple program in Cah ok thanks, but do you know why its saying -1? the constants USHORT_MAX, and ULONG_MAX also appear to have the values of -1(Original post by JGR)
int hasn't been 16 bits wide since the DOS era.
If you want to print an unsigned int, use the %u format code.
You'll most likely get a result of 4294967295.
If you absolutely must have 16 bit wide integers, use a short (and the associated format code). -
Re: problem with a simple program in CHave a read of this http://en.wikipedia.org/wiki/2s_compliment(Original post by Dr Ben)
ah ok thanks, but do you know why its saying -1? the constants USHORT_MAX, and ULONG_MAX also appear to have the values of -1
It explains how signed integers work.
According to the O'Reilly C Programming Language book it should say 65536 instead of -1