When you assign integer literal 999999999999999999
to int a
, it may be truncated if the int
type is not capable of representing that number. The maximum value that can be stored in an int
is std::numeric_limits<int>::max()
and depends on the platform you are compiling for (but must be at least 32767). Your compiler should warn you about such a truncating assignment:
$ g++ -std=c++11 -Wall -Wextra 32689548.cpp -o 32689548
32689548.cpp: In function ‘int main()’:
32689548.cpp:5:13: warning: overflow in implicit constant conversion [-Woverflow]
int a = 999999999999999999;
^
To answer your question, "[is] long long int a=999999999999999
the same as int a=9999999999999999ll
" - why not test it for yourself? Like this, perhaps:
#include <iostream>
int main()
{
long long int a = 999999999999999999;
int b = 999999999999999999ll;
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << (a==b) << std::endl;
return 0;
}
$ g++ -std=c++11 -Wall -Wextra 32689548.cpp -o 32689548
32689548.cpp: In function ‘int main()’:
32689548.cpp:6:13: warning: overflow in implicit constant conversion [-Woverflow]
int b = 999999999999999999ll;
^
$ ./32689548
999999999999999999
-1486618625
0
Here we see that assigning a long long
to int b
causes the same truncation (as GCC warns us it will). However, if we change int b
to auto b
, then we get no warnings and a true comparison:
999999999999999999
999999999999999999
1
For information, I built and ran the above on a system that has
std::numeric_limits<int>::max() = 2147483647
std::numeric_limits<long long int>::max() = 9223372036854775807