Hex value of 6378624653 is : 0x17C32168D But this code prints : 0x7C32168D
#include<iostream>
int main()
{
int x = 6378624653;
printf("0x%x", x);
}
can anyone explain why this happens ? and what should I do to get the right output?
The obtained result means that an object of the type int
can not store such a big value as 6378624653
.
Try the following test program.
#include <iostream>
#include <limits>
int main()
{
std::cout << std::numeric_limits<int>::max() << '\n';
std::cout << 6378624653 << '\n';
std::cout << std::numeric_limits<unsigned int>::max() << '\n';
}
and see what the maximum value that can be stored in an object of the type int
. In most cases using different compilers you will get the following output
2147483647
6378624653
4294967295
That is even objects of the type unsigned int
can not store such value as 6378624653.
You should declare the variable x
as having the type unsigned long long int
.
Here is a demonstration program.
#include <cstdio>
int main()
{
unsigned long long int x = 6378624653;
printf( "%#llx\n", x );
}
The program output is
0x17c32168d
int x{ 6378624653 };
. Then you can see the compiler error.-Wall
%x
format anyways.%x
corresponds tounsigned int
not toint
. Why do you useprintf
instead ofstd::cout
anyways? The latter would automatically apply the correct conversion. Furhtermore forprintf
you should includecstdio
, notiostream
...