Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top