unsigned int range


Format Specifier for unsigned int is %u

#include <stdio.h>
void main ()
{
    char *overflow_val = "4294967297";
    char *neg_value = "-2";
    unsigned int unsigned_val = 0; //Range is from 0 to 4294967295

    printf("overflow_val %s \t neg_val %s\n", overflow_val, neg_value);

    sscanf(overflow_val, "%u",&unsigned_val);
    printf("Unsigned int can't hold value %s which is more than maximum range %u\n", overflow_val, unsigned_val);

    sscanf(neg_value, "%u",&unsigned_val);
    printf("Unsigned int can't hold values lesser than 0 so it returns val %u instead of %s \n", unsigned_val, neg_value);

    return;
}


Output 


overflow_val 4294967297   neg_val -2
Unsigned int can't hold value 4294967297 which is more than maximum range 4294967295
Unsigned int can't hold values lesser than 0 so it returns val 4294967294 instead of -2 


Storing Value       Unsinged int variable

4294967297         4294967295
4294967296         4294967295
4294967295         4294967295
.
.
.
0                           0
-1                          4294967295
-2                          4294967294
.
.
.

Please correct me if this is wrong.

Comments