我误会了,并在' c&#x27中签名值。语言

发布于 2025-02-08 14:55:06 字数 449 浏览 1 评论 0原文

我正在使用Turbo C ++。

这是代码:

#include<stdio.h>
#include<conio.h>
void main()
{
 clrscr();
 
 printf("%d",1000*100);
 printf("\n%d",1000*10);

 getch();
}

输出:

-31072
10000
  1. 为什么第一个printf()给出错误的签名值?整数是否具有范围(-2147483648至+2147483647)。

  2. 和第二个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
  1. Why does the first printf() give wrong and signed value? Whether integer have range (-2147483648 to +2147483647).

  2. And in second printf() it gives right value with right sign. How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽你眉间 2025-02-15 14:55:06

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, include limits.h and check (see the last source).

Hope it helps.

Sources:

月下客 2025-02-15 14:55:06

您有一个 Integer溢出

Turbo C/C ++是16位编译器。它不用于开发。尽快停止使用它。

int类型可以在-3276832767之间存储值。以下程序演示。

#include<stdio.h>
#include<conio.h>

void main()
{
    int i;
    clrscr();
    for(i = 0; i >= 0; i++);
    printf("%d\n", i); // upper limit of int
    printf("%d\n", i - 1); //lower limit of int
    getch();
}

要获取所需的答案,您需要将该号码投入到long中。

printf("%d",1000*100);

应该

printf("%ld", (long)1000*100);

在线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 and 32767. The following program demonstrates.

#include<stdio.h>
#include<conio.h>

void main()
{
    int i;
    clrscr();
    for(i = 0; i >= 0; i++);
    printf("%d\n", i); // upper limit of int
    printf("%d\n", i - 1); //lower limit of int
    getch();
}

To get the answer you want, you need to cast that number to long.

printf("%d",1000*100);

should be

printf("%ld", (long)1000*100);

See it in action at OnlineGDB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文