long integer problem

2019-08-28 12:28发布

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//??

4条回答
时光不老,我们不散
2楼-- · 2019-08-28 12:56

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.

查看更多
在下西门庆
3楼-- · 2019-08-28 13:01

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.

查看更多
Luminary・发光体
4楼-- · 2019-08-28 13:02

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

查看更多
The star\"
5楼-- · 2019-08-28 13:10

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.

查看更多
登录 后发表回答