How to display the maximum value of a unsigned long long in C?

Lazer picture Lazer · Oct 9, 2010 · Viewed 42.6k times · Source

What am I doing wrong here?

$ cat size.c
#include<stdio.h>
#include<math.h>

int main() {

printf ("sizeof unsigned int = %d bytes.\n", sizeof(unsigned int));
printf ("sizeof unsigned long long = %d bytes.\n", sizeof(unsigned long long));

printf ("max unsigned int = %d\n", (int)(pow(2, 32) - 1));
printf ("max unsigned long long = %lld\n", (unsigned long long)(pow(2, 64) - 1));

}
$ gcc size.c -o size
$ ./size
sizeof unsigned int = 4 bytes.
sizeof unsigned long long = 8 bytes.
max unsigned int = 2147483647
max unsigned long long = -1
$ 

I am expecting 18446744073709551615 as output instead of a -1 at the last line.


Okay, I completely missed that I was getting the wrong value for 232 - 1, which should have been 4294967295, not 2147483647. Things make more sense now.

Answer

Jens Gustedt picture Jens Gustedt · Oct 9, 2010

Just don't suppose that it has a certain value use ULLONG_MAX