长整数问题

发布于 2024-08-26 04:18:52 字数 409 浏览 3 评论 0原文

我是 C 初学者,使用 Turbo C++ 编译器(16 位)。

在我正在编写的软件中,最大答案约为 32000。如果我想要一个大于该值的数字,我会使用 long int

如果我执行以下程序:

#include <stdio.h>
void main()
{
    long int x;
    x=40000;
    printf("%d", x);
}

然后我收到一个错误,指出函数 main() 中的常量值很长。

我怎样才能得到超过 32000 的答案并消除这个错误? 另外,当我使用无符号整数时,我将 %d 更改为 %ld 并使用 40000L bt 那么我还需要将 'l' 与 40000 // 一起使用?

I'm a beginner at C, and using Turbo C++ compiler (16 bit).

In the software I'm writing, the maximum answer is around 32000. If I want a number larger than that, I use long int.

If I execute the following program:

#include <stdio.h>
void main()
{
    long int x;
    x=40000;
    printf("%d", x);
}

Then I get an error that the constant value is long in function main().

How can I get an answer more that 32000 and get rid of this error?
also nw i change %d to %ld and use 40000L bt when i use unsigned integer then also i need to use 'l' with 40000//??

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

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

发布评论

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

评论(4

一人独醉 2024-09-02 04:18:52

在 printf 中使用 %ld 表示长整型。 %d 代表 int,它在编译器中只有 16 位。对于错误消息,使用 x=40000L。

Use %ld in printf for the long int. %d is for int which only has 16 bits in your compiler. And for the error message, use x=40000L.

层林尽染 2024-09-02 04:18:52

将 long 更改为 unsigned,40000 将适合 unsigned int。

Change long to unsigned, 40000 will fit in unsigned int.

深海不蓝 2024-09-02 04:18:52

假设您使用的是 Windows,最佳解决方案是针对 32 位或 64 位平台。 16 位程序甚至无法在 64 位版本的 Windows 上运行;你真的应该升级。

Microsoft 有一个免费版本的 Visual Studio:Visual C++ Express Edition 。这也是一个很好的选择,因为它附带了完整的 IDE。

Gcc 也可以以 Mingw 的形式用于 Windows。不幸的是,mingw 本身并没有发布现成的编译器,但其他编译器却发布了,例如 equation.comTDM

Assuming you're on windows, the best solution to this is to target a 32 or 64-bit platform. 16-bit programs won't even run on 64-bit versions of windows; you should really upgrade.

Microsoft has a free version of Visual Studio: Visual C++ Express Edition. This is an excellent option also because it comes with a full IDE.

Gcc is also available for windows in the form of Mingw. Unfortunately, mingw itself does not release ready-to-use compilers, but others do, such as equation.com or TDM.

递刀给你 2024-09-02 04:18:52

也许温习一下 可变参数格式 可能会有所帮助:) 到你(或 printf() 时)子系统)实际上开始扩展可变参数,假设您知道它们是什么类型。

这不仅适用于 printf,还适用于在讨论 printf 时使用 va_*() 或 v*printf() 的任何其他函数。不要忘记你的类型。

另外,请跟踪签名以避免意外结果。

换句话说,当您调用 printf() 或其他任何接受 省略号 时,确定你正在传递什么。这并不限于 printf(),事实上,超出范围通常不会产生编译器警告。

Perhaps brushing up on variadic formatting might help :) By the time you (or the printf() subsystem) actually gets to expanding variadic arguments, its assumed that you know what type they are.

This not only goes for printf, but any other function that employs va_*() or v*printf() when discussing printf. Don't lose track of your types.

Also, keep track of signedness to avoid unexpected results.

In other words, by the time you call printf(), or anything else accepting an elipsis, be sure of what you are passing. This isn't limited to printf(), in fact venturing beyond that will often not produce compiler warnings.

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