tags:

views:

6727

answers:

5

The printf function takes an argument type, such as %d or %i for a signed int. However, I don't see anything for a long value.

A: 

[edit: this is incorrect... see other answers regarding %ld]

I'm pretty sure that %d or %i just signify to treat the input as an integer regardless of its size. So whether it is 8, 16, 32, or 64 bits it will be printed as an integer.

As an aside, on x86 Windows and I believe w/ GCC, ints and longs are actually both 32 bits.

Ben Childs
+13  A: 

Put an 'l' directly before the specifier.

unsigned long n;
printf("%ld", n);
postfuturist
+4  A: 

@steveth45: I think you mean:

unsigned long n;
printf("%lu", n);   // unsigned long

or

long n;
printf("%ld", n);   // signed long

Am I right?

Blorgbeard
+1  A: 

I think to answer this question definitively would require knowing the compiler name and version that you are using and the platform (CPU type, OS etc.) that it is compiling for.

Andrew O'Reilly