long long in C/C++(C/C++ 中的 long long)
问题描述
我正在 GNU 的 C++ 编译器上尝试此代码,但无法理解其行为:
I am trying this code on GNU's C++ compiler and am unable to understand its behaviour:
#include <stdio.h>;
int main()
{
int num1 = 1000000000;
long num2 = 1000000000;
long long num3;
//num3 = 100000000000;
long long num4 = ~0;
printf("%u %u %u", sizeof(num1), sizeof(num2), sizeof(num3));
printf("%d %ld %lld %llu", num1, num2, num3, num4);
return 0;
}
当我取消注释注释行时,代码无法编译并给出错误:
When I uncomment the commented line, the code doesn't compile and is giving an error:
错误:整数常量对于长类型来说太大
error: integer constant is too large for long type
但是,如果代码按原样编译并执行,它会产生远大于 10000000000 的值.
But, if the code is compiled as it is and is executed, it produces values much larger than 10000000000.
为什么?
推荐答案
字母 100000000000 组成了一个文字整数常量,但对于 int
类型来说该值太大了.您需要使用后缀来更改文字的类型,即
The letters 100000000000 make up a literal integer constant, but the value is too large for the type int
. You need to use a suffix to change the type of the literal, i.e.
long long num3 = 100000000000LL;
后缀LL
使文字变成long long
类型.C 不够聪明",无法从左侧的类型得出结论,类型是文字本身的属性,而不是使用它的上下文.
The suffix LL
makes the literal into type long long
. C is not "smart" enough to conclude this from the type on the left, the type is a property of the literal itself, not the context in which it is being used.
这篇关于C/C++ 中的 long long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C/C++ 中的 long long


基础教程推荐
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01