C++ literal integer type

后端 未结 2 1107
南旧
南旧 2020-12-19 05:36

Do literal expressions have types too ?

long long int a = 2147483647+1 ;
long long int b = 2147483648+1 ; 
std::cout << a << \',\' << b ; /         


        
2条回答
  •  清歌不尽
    2020-12-19 06:22

    int a = INT_MAX ;
    long long int b = a + 1 ; // adds 1 to a and convert it then to long long ing
    long long int c = a; ++c; // convert a to long long int and increment the result with 1
    cout << a << std::endl; // 2147483647
    cout << b << std::endl; // -2147483648
    cout << c << std::endl; // 2147483648
    cout << 2147483647 + 1 << std::endl; // -2147483648 (by default integer literal is assumed to be int)
    cout << 2147483647LL + 1 << std::endl; // 2147483648 (force the the integer literal to be interpreted as a long long int)
    

    You can find more information about integer literals here.

提交回复
热议问题