Node:Formatted output conversion specifiers, Previous:printf, Up:Formatted string output

Formatted output conversion specifiers

There are many different conversion specifiers that can be used for various data types. Conversion specifiers can become quite complex; for example, %-17.7ld specifies that printf should print the number left-justified (-), in a field at least seventeen characters wide (17), with a minimum of seven digits (.7), and that the number is a long integer (l) and should be printed in decimal notation (%d).

In this section, we will examine the basics of printf and its conversion specifiers. (For even more detail, Formatted Output.)

A conversion specifier begins with a percent sign, and ends with one of the following output conversion characters. The most basic conversion specifiers simply use a percent sign and one of these characters, such as %d to print an integer. (Note that characters in the template string that are not part of a conversion specifier are printed as-is.)

Print a single character.
Print an integer as a signed decimal number.
Print a floating-point number in exponential notation, using lower-case letters. The exponent always contains at least two digits. Example: 6.02e23.
Same as e, but uses upper-case letters. Example: 6.02E23.
Print a floating-point number in normal, fixed-point notation.
Same as d.
Print the string corresponding to the specified value of the system errno variable. (See Usual file name errors.) GNU systems only.
Print a string.
Print an unsigned integer.
Print an integer as an unsigned hexadecimal number, using lower-case letters.
Same as x, but uses upper-case letters.
Print a percent sign (%).

In between the percent sign (%) and the output conversion character, you can place some combination of the following modifiers. (Note that the percent sign conversion (%%) doesn't use arguments or modifiers.)

Make sure that your conversion specifiers use valid syntax; if they do not, if you do not supply enough arguments for all conversion specifiers, or if any arguments are of the wrong type, unpredictable results may follow. Supplying too many arguments is not a problem, however; the extra arguments are simply ignored.

Here is a code example that shows various uses of printf.

#include <stdio.h>
#include <errno.h>

int main()
  int my_integer = -42;
  unsigned int my_ui = 23;
  float my_float = 3.56;
  double my_double = 424242.171717;
  char my_char = 'w';
  char my_string[] = "Pardon me, may I borrow your nose?";

  printf ("Integer: %d\n", my_integer);
  printf ("Unsigned integer: %u\n", my_ui);

  printf ("The same, as hexadecimal: %#x %#x\n", my_integer, my_ui);

  printf ("Floating-point: %f\n", my_float);
  printf ("Double, exponential notation: %17.11e\n", my_double);

  printf ("Single character: %c\n", my_char);
  printf ("String: %s\n", my_string);

  errno = EACCES;
  printf ("errno string (EACCES): %m\n");

  return 0;

The code example above produces the following output on a GNU system:

Integer: -42
Unsigned integer: 23
The same, as hexadecimal: 0xffffffd6 0x17
Floating-point: 3.560000
Double, exponential notation: 4.24242171717e+05
Single character: w
String: Pardon me, may I borrow your nose?
errno string (EACCES): Permission denied