[ACCEPTED]-Calculating and using maximum value of uint32_t-c99

Accepted answer
Score: 13

The portable way to print a uintN_t object is to 9 cast it to a uintmax_t and use the j length modifier 8 with the u conversion specifier:

printf("%ju\n", (uintmax_t)(UINT32_MAX));

The j means 7 that the argument is either an intmax_t or a uintmax_t; the 6 u means it is unsigned, so it is a uintmax_t.

Or, you 5 can use the format strings defined in <inttypes.h> (n 4 this case, you'd use PRIu32):

printf("%" PRIu32 "\n", UINT32_MAX);

You can't just use 3 %u because it isn't guaranteed that int is represented 2 by at least 32 bits (it only needs to be 1 represented by at least 16 bits).

Score: 4

You encountered your specific problem because 4 %d is a signed formatter.

There are a number 3 of ways to fix it (two have already been 2 suggested), but the really correct way is to use 1 the format specifiers defined in <inttypes.h>:

uint32_t number;
printf("number is %" PRIu32 "\n", number);
Score: 2

%d is for signed integers. Use %u.

EDIT: Ignore 2 this answer and use James's, which is more 1 complete.

Score: 0

If you're setting an array of unsigned int 2 to the max values you could do it via memset:

memset(array, 0xFF, sizeof(unsigned 1 int) * arraysize);

More Related questions