I want to print out an integer type variable which value is 3000000000;
So I wrote the below code then ran it, but the printed value was incorrect. I think the variable is overflowed. But I don't know why.
#include<stdio.h>
int main(void) {
unsigned int num1 = 3000000000;
printf("%d", num1);
}
As far as I know, the maximum value of unsigned integer type variable is (2^32-1 = 4,294,967,296 - 1) when the code complies on Win32 API. But the printed value is -1294967296.
I have no idea why overflow occurs in my code.
If anyone knows the reason, please let me know :)
Best regards,
- I use Microsoft Visual Studio 2015 Professional.
%uinstead of%d.%dis for signedints. It will take whatever value it finds and interpret it assigned intand print it. Tryunsigned int num1 = 0xffffffff; printf("%d", num1);. It will be more obvious.4294967295=0xffffffff