Questions tagged [printf]

`printf` is a common function for formatted output. C and many other languages have a whole family of related functions. Only use this tag if the question is directly concerned with `printf` or related functions.

For detailed information on how the printf function works in C, refer to Open Group Base Specifications or the C standard.

C99 knows these members of the printf()-family:

  printf     vprintf     wprintf     vwprintf
 fprintf    vfprintf    fwprintf    vfwprintf
 sprintf    vsprintf    swprintf    vswprintf
snprintf   vsnprintf   snwprintf   vsnwprintf

POSIX additionally knows these:

 dprintf    vdprintf

GNU adds these:

asprintf   vasprintf

The name is generally of the form:

  • optional v for va_list
  • output specifier: None for stdout, f for FILE*, s for supplied buffer, sn for supplied buffer of specified length, as for allocated buffer, d for supplied file descriptor
  • optional w for wide variant.
  • printf

Documentation for printf function is available on Linux/Unix in section 3 of the manual and can be accessed by using man 3 printf.

For a multi-lingual description of the history of printing formatted strings across many languages, refer to Wikipedia article printf format string

8439 questions
2133
votes
52 answers

JavaScript equivalent to printf/String.Format

I'm looking for a good JavaScript equivalent of the C/PHP printf() or for C#/Java programmers, String.Format() (IFormatProvider for .NET). My basic requirement is a thousand separator format for numbers for now, but something that handles lots of…
Chris S
  • 62,476
  • 49
  • 214
  • 238
582
votes
10 answers

Why does printf not flush after the call unless a newline is in the format string?

Why does printf not flush after the call unless a newline is in the format string? Is this POSIX behavior? How might I have printf immediately flush every time?
Crazy Chenz
  • 10,877
  • 12
  • 44
  • 57
542
votes
8 answers

What is the printf format specifier for bool?

Since ANSI C99 there is _Bool or bool via stdbool.h. But is there also a printf format specifier for bool? I mean something like in that pseudo code: bool x = true; printf("%B\n", x); which would print: true
maxschlepzig
  • 27,589
  • 9
  • 109
  • 146
532
votes
7 answers

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.
Thomas Owens
  • 107,741
  • 94
  • 299
  • 427
523
votes
5 answers

Correct format specifier for double in printf

What is the correct format specifier for double in printf? Is it %f or is it %lf? I believe it's %f, but I am not sure. Code sample #include int main() { double d = 1.4; printf("%lf", d); // Is this wrong? }
Leopard
  • 5,241
  • 3
  • 13
  • 4
479
votes
54 answers

Is there a printf converter to print in binary format?

I can print with printf as a hex or octal number. Is there a format tag to print as binary, or arbitrary base? I am running gcc. printf("%d %x %o\n", 10, 10, 10); //prints "10 A 12\n" print("%b\n", 10); // prints "%b\n"
Brian
  • 7,442
  • 5
  • 25
  • 27
443
votes
12 answers

How can one print a size_t variable portably using the printf family?

I have a variable of type size_t, and I want to print it using printf(). What format specifier do I use to print it portably? In 32-bit machine, %u seems right. I compiled with g++ -g -W -Wall -Werror -ansi -pedantic, and there was no warning. But…
Arun
  • 18,092
  • 8
  • 46
  • 58
413
votes
16 answers

'printf' vs. 'cout' in C++

What is the difference between printf() and cout in C++?
hero
  • 4,171
  • 3
  • 14
  • 6
403
votes
13 answers

How do you format an unsigned long long int using printf?

#include 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.\n", sizeof(num), num, normalInt); …
andrewrk
  • 27,002
  • 25
  • 87
  • 105
377
votes
11 answers

Printing leading 0's in C

I'm trying to find a good way to print leading 0, such as 01001 for a ZIP Code. While the number would be stored as 1001, what is a good way to do it? I thought of using either case statements or if to figure out how many digits the number is and…
zxcv
  • 6,921
  • 8
  • 32
  • 30
374
votes
7 answers

How to printf "unsigned long" in C?

I can never understand how to print unsigned long datatype in C. Suppose unsigned_foo is an unsigned long, then I try: printf("%lu\n", unsigned_foo) printf("%du\n", unsigned_foo) printf("%ud\n", unsigned_foo) printf("%ll\n",…
bodacydo
  • 63,809
  • 83
  • 206
  • 303
323
votes
106 answers

Printing 1 to 1000 without loop or conditionals

Task: Print numbers from 1 to 1000 without using any loop or conditional statements. Don't just write the printf() or cout statement 1000 times. How would you do that using C or C++?
Saurabh Gokhale
  • 311
  • 3
  • 4
  • 5
300
votes
13 answers

How to escape the % (percent) sign in C's printf

How do you escape the % sign when using printf in C? printf("hello\%"); /* not like this */
Chris_45
  • 7,981
  • 16
  • 56
  • 72
248
votes
8 answers

Difference between fprintf, printf and sprintf?

Can anyone explain in simple English about the differences between printf, fprintf, and sprintf with examples? What stream is it in? I'm really confused between the three of these while reading about "File Handling in C".
Vishwanath Dalvi
  • 31,604
  • 36
  • 115
  • 146
218
votes
5 answers

printf() formatting for hexadecimal

Why, when printing a number in hexadecimal as an 8 digit number with leading zeros, does %#08X not display the same result as 0x%08X? When I try to use the former, the 08 formatting flag is removed, and it doesn't work with just 8.
wsmccusker
  • 2,201
  • 2
  • 12
  • 8
1
2 3
99 100