Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the argument for printf that formats a long?

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.

like image 209
Thomas Owens Avatar asked Sep 01 '08 22:09

Thomas Owens


People also ask

How do I print a long in printf?

To print a long value, use the %ld format specifier. You can use the l prefix for x and o, too. So you would use %lx to print a long integer in hexadecimal format and %lo to print in octal format. C allows both uppercase and lowercase letters for constant suffixes, these format specifiers use just lowercase.

What is argument in printf?

The parameters passed into printf() are known as arguments; these are separated commas. C Program 2.1 contains a printf() statement with only one argument, that is, a text string. This string is referred to as the message string and is always the first argument of printf().

How do I print long long int in printf?

#include <stdio. h> int main() { unsigned long long int num = 285212672; //FYI: fits in 29 bits int normalInt = 5; printf("My number is %d bytes wide and its value is %ul. A normal number is %d.


6 Answers

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

printf("%lu %ld", n, m);
like image 114
postfuturist Avatar answered Oct 06 '22 06:10

postfuturist


I think you mean:

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

or

long n;
printf("%ld", n);   // signed long
like image 40
Blorgbeard Avatar answered Oct 06 '22 07:10

Blorgbeard


On most platforms, long and int are the same size (32 bits). Still, it does have its own format specifier:

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

For 64 bits, you'd want a long long:

long long n;
unsigned long long un;
printf("%lld", n); // signed
printf("%llu", un); // unsigned

Oh, and of course, it's different in Windows:

printf("%l64d", n); // signed
printf("%l64u", un); // unsigned

Frequently, when I'm printing 64-bit values, I find it helpful to print them in hex (usually with numbers that big, they are pointers or bit fields).

unsigned long long n;
printf("0x%016llX", n); // "0x" followed by "0-padded", "16 char wide", "long long", "HEX with 0-9A-F"

will print:

0x00000000DEADBEEF

Btw, "long" doesn't mean that much anymore (on mainstream x64). "int" is the platform default int size, typically 32 bits. "long" is usually the same size. However, they have different portability semantics on older platforms (and modern embedded platforms!). "long long" is a 64-bit number and usually what people meant to use unless they really really knew what they were doing editing a piece of x-platform portable code. Even then, they probably would have used a macro instead to capture the semantic meaning of the type (eg uint64_t).

char c;       // 8 bits
short s;      // 16 bits
int i;        // 32 bits (on modern platforms)
long l;       // 32 bits
long long ll; // 64 bits 

Back in the day, "int" was 16 bits. You'd think it would now be 64 bits, but no, that would have caused insane portability issues. Of course, even this is a simplification of the arcane and history-rich truth. See wiki:Integer

like image 31
Dave Dopson Avatar answered Oct 06 '22 05:10

Dave Dopson


It depends, if you are referring to unsigned long the formatting character is "%lu". If you're referring to signed long the formatting character is "%ld".

like image 34
krato Avatar answered Oct 06 '22 05:10

krato


%ld see printf reference on cplusplus.com

like image 41
Rob Walker Avatar answered Oct 06 '22 07:10

Rob Walker


In case you're looking to print unsigned long long as I was, use:

unsigned long long n;
printf("%llu", n);

For all other combinations, I believe you use the table from the printf manual, taking the row, then column label for whatever type you're trying to print (as I do with printf("%llu", n) above).

like image 37
Dolan Antenucci Avatar answered Oct 06 '22 07:10

Dolan Antenucci