我误会了,并在' c&#x27中签名值。语言
我正在使用Turbo C ++。
这是代码:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("%d",1000*100);
printf("\n%d",1000*10);
getch();
}
输出:
-31072
10000
为什么第一个
printf()
给出错误的签名值?整数是否具有范围(-2147483648至+2147483647)。和第二个
printf()
它给出了正确的符号的正确值。如何?
I am using Turbo C++.
This is code:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("%d",1000*100);
printf("\n%d",1000*10);
getch();
}
Output:
-31072
10000
Why does the first
printf()
give wrong and signed value? Whether integer have range (-2147483648 to +2147483647).And in second
printf()
it gives right value with right sign. How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1000*100 = 100'000。如果您的
int
是16位长,则结果比最大支持值高,即32'767。如果您正在运行32位OS或制作32位程序,请考虑使用“ Long”,在32位OSS的情况下为4个字节或64位的字节为8个字节;最高支持的值将分别为:2'147'483'647,9'223'372'036'854'775'807。要将
长
作为输出,请使用以下格式指定器之一:%l
,%ld
或%li 。
要查看
int
的最大值,请包括limits.h
并检查(请参阅最后一个源)。希望它有帮助。
资料来源:
1000*100=100'000. If your
int
is 16-bits-long, then the result is higher than the maximum supported value, which is 32'767. If you're running a 32-bit OS or you're making a 32-bit program, consider using "long", which is 4 bytes in the case of 32-bits OSs or 8 bytes for 64-bit ones; the highest supported value will respectively be: 2'147'483'647, 9'223'372'036'854'775'807.To have a
long
as an output, use one of the following format specifiers:%l
,%ld
or%li
.To see the maximum value for
int
, includelimits.h
and check (see the last source).Hope it helps.
Sources:
您有一个 Integer溢出。
Turbo C/C ++是16位编译器。它不用于开发。尽快停止使用它。
int
类型可以在-32768
和32767
之间存储值。以下程序演示。要获取所需的答案,您需要将该号码投入到
long
中。应该
在在线gdb 上看到它。
You are having an integer overflow.
Turbo C/C++ is a 16-bit compiler. It is not used for development. Stop using it as soon as you can.
The
int
type can store values between-32768
and32767
. The following program demonstrates.To get the answer you want, you need to cast that number to
long
.should be
See it in action at OnlineGDB.