C语言 在 C 中打印 long int 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17779570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing long int value in C
提问by Jijo Jose
I have two variables of long inttype as shown below:
我有两个long int类型的变量,如下所示:
long int a=-2147483648, b=-2147483648;
a=a+b;
printf("%d",a);
I am getting zero. I tried changing the type to long long int, but I'm still not getting the correct answer.
我得到零。我尝试将类型更改为long long int,但仍然没有得到正确答案。
回答by caf
You must use %ldto print a long int, and %lldto print a long long int.
您必须使用%ld打印 a long int,并%lld打印long long int.
Note that only long long intis guaranteed to be large enough to store the result of that calculation (or, indeed, the input values you're using).
请注意, onlylong long int保证足够大以存储该计算的结果(或者,实际上,您正在使用的输入值)。
You will also need to ensure that you use your compiler in a C99-compatible mode (for example, using the -std=gnu99option to gcc). This is because the long long inttype was not introduced until C99; and although many compilers implement long long intin C90 mode as an extension, the constant 2147483648may have a type of unsigned intor unsigned longin C90. If this is the case in your implementation, then the value of -2147483648will also have unsigned type and will therefore be positive, and the overall result will be not what you expect.
您还需要确保在 C99 兼容模式下使用编译器(例如,使用-std=gnu99gcc 选项)。这是因为该long long int类型直到 C99 才被引入;尽管许多编译器long long int在 C90 模式下作为扩展实现,但常量2147483648可能具有C90 中的unsigned int或类型unsigned long。如果在您的实现中是这种情况,则 的值也-2147483648将具有无符号类型,因此将为正数,并且总体结果将不是您所期望的。
回答by P0W
回答by Maniruzzaman Akash
To take input " long int" and output " long int" in C is :
在 C 中输入“ long int”并输出“ long int”是:
long int n;
scanf("%ld", &n);
printf("%ld", n);
To take input " long long int" and output " long long int" in C is :
在 C 中输入“ long long int”并输出“ long long int”是:
long long int n;
scanf("%lld", &n);
printf("%lld", n);
Hope you've cleared..
希望你已经清除了..

