Friday 15 January 2010

unsigned integer getting overflowed in c -



unsigned integer getting overflowed in c -

this question has reply here:

unsigned int in c behaves negative 8 answers

i wrote simple c programme find maximum positive number can reach using unsigned integer given below. size of integer on machine 4 bytes.

#include <stdio.h> #include <math.h> main() { unsigned int x = 1; int = 1; for(; <= 31; i++) { x = x * 2; } unsigned int y = pow(2, 31); printf("%d\n", x); printf("%d\n", y); }

both x, y getting overflowed , value -2147483648. thought should not overflow because on machines sizeof(int) = 4 bytes, unsigned int range should pow(2, 32) - 1. can 1 please allow me know why getting overflowed?

you want utilize %u unsigned int:

printf("%u\n", x); printf("%u\n", y);

c

No comments:

Post a Comment