c++ - int variable is not able to store large values -
#include <iostream> int main() { int = 999999999999999999; std::cout << << std::endl; std::cout << 999999999999999999 << std::endl; return 0; } the output of above program -1486618625 , 999999999999999999.
in both cout giving same number, why outputs different?
also long long int a=999999999999999 same int a=9999999999999999ll?
when assign integer literal 999999999999999999 int a, may truncated if int type not capable of representing number. maximum value can stored in int std::numeric_limits<int>::max() , depends on platform compiling (but must @ least 32767). compiler should warn such 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 = 999999999999999999; ^ to answer question, "[is] long long int a=999999999999999 same int a=9999999999999999ll" - why not test yourself? this, perhaps:
#include <iostream> int main() { long long int = 999999999999999999; int b = 999999999999999999ll; std::cout << << 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 see assigning long long int b causes same truncation (as gcc warns will). however, if change int b auto b, no warnings , true comparison:
999999999999999999 999999999999999999 1 for information, built , ran above on system has
std::numeric_limits<int>::max() = 2147483647 std::numeric_limits<long long int>::max() = 9223372036854775807
Comments
Post a Comment