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

9422 questions
61 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
  • 64,770
  • 52
  • 221
  • 239
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
  • 12,650
  • 12
  • 50
  • 62
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
  • 35,645
  • 14
  • 145
  • 182
7 answers

What is the conversion specifier 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
  • 114,398
  • 98
  • 311
  • 431
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? }
  • 5,851
  • 3
  • 15
  • 4
58 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" printf("%b\n", 10); // prints "%b\n"
  • 8,454
  • 5
  • 27
  • 30
16 answers

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

What is the difference between printf() and cout in C++?
  • 5,161
  • 3
  • 16
  • 6
14 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…
  • 19,750
  • 10
  • 51
  • 60
14 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); …
  • 30,272
  • 27
  • 92
  • 113
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",…
  • 75,521
  • 93
  • 229
  • 319
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…
  • 7,391
  • 8
  • 34
  • 30
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 */
  • 8,769
  • 16
  • 62
  • 73
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
9 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
  • 35,388
  • 41
  • 123
  • 155
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.
  • 2,631
  • 2
  • 14
  • 8
2 3
99 100