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
241
votes
18 answers

How to print a float with 2 decimal places in Java?

Can I do it with System.out.print?
via_point
  • 2,563
  • 3
  • 17
  • 9
235
votes
5 answers

PHP sprintf escaping %

I want the following output:- About to deduct 50% of € 27.59 from your Top-Up account. when I do something like this:- $variablesArray[0] = '€'; $variablesArray[1] = 27.59; $stringWithVariables = 'About to deduct 50% of %s %s from your Top-Up…
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144
228
votes
4 answers

What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf)

What is the difference between %d and %i when used as format specifiers in printf and scanf?
Ayoub M.
  • 4,690
  • 10
  • 42
  • 52
214
votes
24 answers

Why use sprintf function in PHP?

I am trying to learn more about the PHP function sprintf() but php.net did not help me much as I am still confused, why would you want to use it? Take a look at my example below. Why use this: $output = sprintf("Here is the result: %s for this…
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
212
votes
10 answers

What is the difference between printf() and puts() in C?

I know you can print with printf() and puts(). I can also see that printf() allows you to interpolate variables and do formatting. Is puts() merely a primitive version of printf(). Should it be used for every possible printf() without string…
alex
  • 479,566
  • 201
  • 878
  • 984
210
votes
9 answers

printf with std::string?

My understanding is that string is a member of the std namespace, so why does the following occur? #include int main() { using namespace std; string myString = "Press ENTER to quit program!"; cout << "Come up and C++ me some…
Chunky Chunk
  • 16,553
  • 15
  • 84
  • 162
200
votes
7 answers

How can I print to standard error in C with 'printf'?

In C, printing to standard output (stdout) is easy, with printf from stdio.h. However, how can I print to standard error (stderr)? We can use fprintf to achieve it apparently, but its syntax seems strange. Maybe we can use printf to print to…
wad
  • 2,491
  • 3
  • 14
  • 16
178
votes
11 answers

How do you allow spaces to be entered using scanf?

Using the following code: char *name = malloc(sizeof(char) + 256); printf("What is your name? "); scanf("%s", name); printf("Hello %s. Nice to meet you.\n", name); A user can enter their name but when they enter a name with a space like Lucas…
Kredns
  • 36,461
  • 52
  • 152
  • 203
172
votes
8 answers

Is there a way to specify how many characters of a string to print out using printf()?

Is there a way to specify how many characters of a string to print out (similar to decimal places in ints)? printf ("Here are the first 8 chars: %s\n", "A string that is more than 8 chars"); Would like it to print: Here are the first 8 chars: A…
T.T.T.
  • 33,367
  • 47
  • 130
  • 168
163
votes
4 answers

What does "%.*s" mean in printf?

I got a code snippet in which there is a printf("%.*s\n") what does the %.*s mean?
StevenWang
  • 3,625
  • 4
  • 30
  • 40
159
votes
4 answers

Java: Literal percent sign in printf statement

I'm trying to add an actual percent sign into a printf statement in Java and I'm getting the error: lab1.java:166: illegal escape character System.out.printf("%s\t%s\t%1.2f\%\t%1.2f\%\n",ID,pattern,support,confidence); …
Dom M.
  • 3,762
  • 8
  • 32
  • 40
157
votes
4 answers

Left-pad printf with spaces

How can I pad a string with spaces on the left when using printf? For example, I want to print "Hello" with 40 spaces preceding it. Also, the string I want to print consists of multiple lines. Do I need to print each line separately? EDIT: Just to…
titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133
156
votes
12 answers

What is the use of the %n format specifier in C?

What is the use of the %n format specifier in C? Could anyone explain with an example?
josh
  • 13,793
  • 12
  • 49
  • 58
156
votes
9 answers

Printf width specifier to maintain precision of floating-point value

Is there a printf width specifier which can be applied to a floating point specifier that would automatically format the output to the necessary number of significant digits such that when scanning the string back in, the original floating point…
Vilhelm Gray
  • 11,516
  • 10
  • 61
  • 114
144
votes
8 answers

What's up with Java's "%n" in printf?

I'm reading Effective Java and it uses %n for the newline character everywhere. I have used \n rather successfully for newline in Java programs. Which is the 'correct' one? What's wrong with \n ? Why did Java change this C convention?
ripper234
  • 222,824
  • 274
  • 634
  • 905