How can I print maximum value of an unsigned integer?

Use %u as the printf format string.


The %d format treats its argument as a signed int. Use %u instead.

But a better way to get the maximum value of type unsigned int is to use the UINT_MAX macro. You'll need

#include <limits.h>

to make it visible.

You can also compute the maximum value of an unsigned type by converting the value -1 to the type.

#include <limits.h>
#include <stdio.h>
int main(void) {
    unsigned int max = -1;
    printf("UINT_MAX = %u = 0x%x\n", UINT_MAX, UINT_MAX);
    printf("max      = %u = 0x%x\n", max, max);
    return 0;
}

Note that the UINT_MAX isn't necessarily 0xffffffff. It is if unsigned int happens to be 32 bits, but it could be as small as 16 bits; it's 64 bits on a few systems.

Tags:

C