I was trying to figure out the minimum and maximum value for an unsigned data type. I know that the minimum unsigned value is 0 and the maximum value is (2^n)-1. However, when I tried to run my program (I cannot post my code, but you can refer to this), I keep getting -1 as the maximum value. Can someone explain to me why? Also, UINT_MAX is giving me 4294967295 while ULLONG_MAX is 4294967295. However, the maximum value of unsigned int is supposed to be 65535 while unsigned long long int is supposed to be +18,446,744,073,709,551,615.Why is it the output is different?
Asked
Active
Viewed 3,485 times
-1
-
3Maybe you are printing it with wrong format specifier? – user2736738 Jan 23 '18 at 19:03
-
3UINT_MAX is the same bit-wise as -1 in two's complement notation. You're probably printing it out as a signed integer. – Christian Gibbons Jan 23 '18 at 19:05
1 Answers
6
Whats the format specifier are you using to print those values ? These kind of error mostly occur due to wrong format specifier.
#include <stdio.h>
#include <limits.h>
int main()
{
printf("%u", UINT_MAX); // This will print 4294967295 (system dependent)
printf("%d", UINT_MAX); // This will print -1
return 0;
}

sameer chaudhari
- 928
- 7
- 14
-
2`printf("%d", UINT_MAX); // This will print -1` This is also system dependent as not every system is two's complement. – Christian Gibbons Jan 23 '18 at 19:33
-
2@ChristianGibbons: While in theory non-twos-complement machines can exist, you won't find one outside a museum. – Chris Dodd Jan 23 '18 at 19:42
-
1
-
the `%d` ver is undefined behaviour so could print anything , or format your hard drive – M.M Jan 23 '18 at 23:46
-