This could have been banner-node.jpg
January 2007
28
From Joshua

printf format specifiers

printf() syntax and format specifiers



printf Format Specifiers

print format specifiers have the following form

% [flags] [width] [.prec] [F|N|h|l|L] type_char

Each format specifier begins with the percent character (%). After the % come the following optional specifiers, in this order:

Component

What it Controls or Specifies

[flags] Flag character(s)
Output justification, numeric signs, decimal points, trailing zeros, octal and hex prefixes
  - Left-justifies the result, pads on the right with blanks. If not Given, it right-justifies the result, pads on the left with zeros or Blanks.
  + Signed conversion results always begin with a plus or minus sign. Note: Plus (+) takes precedence over blank if both are given.
  blank If value is nonnegative, the output begins with a blank instead of a plus; negative values still begin with a minus.
  # Specifies that arg is to be converted using an alternate form.
c s d I u
No effect.
0
0 is prepended to a nonzero arg.
x X
0x (or 0X) is prepended to arg.
e E f
The result always contains a decimal point even if no digits follow the point. Normally, a decimal point appears in these results only if a digit follows it.
g G
Same as e and E, except that trailing zeros are not removed.
[width] Width specifier
Minimum number of characters to print, padding with blanks or zeros
  n At least n characters are printed. If the output value has less than n characters, the output is padded with blanks (right-padded if minus (-) flag given, left-padded otherwise).
  0n At least n characters are printed. If the output value has less than n characters, it is filled on the left with zeros.
  * The argument list supplies the width specifier, which must precede the actual argument being formatted.
[.prec] Precision specifier
Maximum number of characters to print; for integers, minimum number Of digits to print
  none Precision set to default:
d,i,o,u,x,X types
1
e,E,f types
6
g,G types
all significant digits
s types
Print to first null character
c types
No effect
  .0 For d,i,o,u,x types, precision set to default, for e,E,f types, no decimal point is printed.
  .n n characters or n decimal places are printed. If the output value has more than n characters, the output might be truncated or rounded. (This depends on the type character.)
d i o u x X
Specifies that at least n digits are printed. If input argument has less than n digits, output value is left-padded with zeros. If input argument has more than n digits, output value is not truncated.
e E f
Specifies that n characters are printed after the decimal point, and the last digit printed is rounded.
g G
Specifies that at most n significant digits are printed.
c
Has no effect on the output.
s
Specifies that no more than n characters are printed.
  .* The argument list supplies the precision specifier, which must precede the actual argument being formatted.
  No numeric characters will be output for a field (i.e., the field will be blank) if the following conditions are all met:
  • you specify an explicit precision of 0
  • the format specifier for the field is one of the integer formats
  • the value to be printed is 0
If you use an * for the precision specifier, the next argument in the call (treated as an int) specifies the precision. If you use asterisks for the width or the precision, or for both, the width argument must immediately follow the specifiers, followed by the precision argument, then the argument for the data to be converted.
[F|N|h|l|L] Input size modifier
Override default size of next input argument:
N = near pointer
F = far pointer
h = short int
l = long
L = long double
These modifiers affect how all the printf functions interpret the data type of the corresponding input argument arg. Both F and N reinterpret the input variable arg. Normally, the arg for a %p, %s, or %n conversion is a pointer of the default size for the memory model. h, l, and L override the default size of the numeric data input arguments. Neither h nor l affects character or pointer types.
type_char Conversion type character
  d Integer Signed decimal integer
  i Integer Signed decimal integer
  o Integer Unsigned octal integer
  u Integer Unsigned decimal integer
  x Integer Unsigned hexadecimal int (with a, b, c, d, e, f)
  X Integer Unsigned hexadecimal int (with A, B, C, D, E, F)
  f Float Signed value of the form [-]dddd.dddd. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.
  e Float Signed value of the form [-]d.dddd or e[+/-]ddd
  g Float Signed value in either e or f form, based on given value and precision. Trailing zeros and the decimal point are printed if necessary.
  E, G Float Same as e with E for exponent. Same as g with E for exponent if e format used
  c Character Single character
  C Character Single wide character
  s String Expects char *. Prints characters until a null-terminator is encountered or precision is reached
  S String Expects wchar *. Specifies a wide-character string. Prints characters until a null-terminator is encountered or precision is reached
  % None Prints the % character
  n int * Stores (in the location pointed to by the input argument) a count of the chars written so far.
  p Pointer Prints the input argument as a pointer; format depends on which memory model was used. It will be either XXXX:YYYY or YYYY (offset only).
  Infinite floating-point numbers are printed as +INF and -INF. An IEEE Not-A-Number is printed as +NAN or -NAN.