views:

115

answers:

4

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

A: 

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

Martin
bt what is the problem with long??
hopefulLLl
You specified "long int x", and specified the default size of int with "%d", which is 16 bits with Turbo C. It's complaining because if you set the value of a long to 70000 and tried to print with %d it'd ignore the high 16 bits to show 4464 instead of 70000.
Arthur Kalliokoski
+6  A: 

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.

Secure
so every tym i need to use 'L'??even if i use unsigne long integer??or it is just for the long int?
hopefulLLl
It is for the long int. For unsigned longs use 40000UL. Better yet, use another compiler as already suggested. And learn about the different integer types, signed and unsigned, the guaranteed bit numbers, promotion rules, and all the correlated stuff like the printf format specifiers.
Secure
A: 

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.

Eamon Nerbonne
A: 

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.

Tim Post