When i storing a large positive integer in unsigned long int in c, then it unknowingly converting to negative number.
for example
a=2075000020, b=10000
You cannot have been printing an unsigned integer, because it has printed a sign. Even if you declare the variable as unsigned
, once it is on the stack for printf()
to use, it is interpreted as a binary value to be used as specified by the format in printf()
.
Note the difference between these, and the results. In the the third example, you can see that bit 31 is set, which is the sign bit for signed long int
.
#include
#include
int main () {
unsigned long int a=2075000020, b=100000000, c;
c = a + b;
printf ("Signed %ld\n", c);
printf ("Unsigned %lu\n", c);
printf ("Hexadecimal 0x%lX\n", c);
return 0;
}
Program output:
Signed -2119967276
Unsigned 2175000020
Hexadecimal 0x81A3DDD4