Keywords: lpg

Next: 8.1.2 Formatted Input Up: 8.1 I/O Function in Previous: 8.1 I/O Function in

## 8.1.1 Formatted Output

The printf(...) functions in provide formatted output and allow transformations of the arguments.

- `int fprintf(FILE *stream, const char *format, ...),`
  will transform the output (arguments to fill in `...`) and write it to `stream`. The format defined in `format` will be written, too. The function will return the number of written characters or a negative value on error.

  `format` contains two kinds of objects

  1. normal characters for the output and
  2. information how to transform or format the arguments.

  Format information must begin with % followed by values for the format followed by a character for the translation (to print % by itself use %%). Possible values for the format are:

  - Flags
    - `-`
      The formatted argument will be printed on the left margin (default is the right margin in the argument field).
    - `+`
      Every number will be printed with a sign, e.g. `+12` or `-2.32`.
  - Blank
    When the first character is not a sign, a blank will be inserted.
  - `0`
    For numeric transformation the field width will be filled up with `0`'s on the left side.
  - `#`
    Alternate output depending on the transformation for the argument
    - For o the first number is a 0.
    - For x or X 0x or 0X will be printed in front of the argument.
    - For e, E, f or F the output has a decimal point.
    - For g or G zeroes on the end of the argument are printed.
  - A number for the minimal field width.
    The transformed argument is printed in a field which is at least as big as the argument itself. With a number you can make the field width bigger. If the formatted argument is smaller, then the field width will be filled with zeroes or blanks.
  - A point to separate the field width and the precision.
  - A number for the precision.

  Possible values for the transformation are in table 8.1 on page .

- `int printf(const char *format, ...)`
  Same as fprintf(stdout, ...).
- `int sprintf(char *s, const char *format, ...)`
  Same as printf(...), except that the output will be written to the the character pointer `s` (with a following `\`0).

  (Note: You must allocate enough memory for `s`.)

- `vprintf(const char *format, va_list arg)`
  `vfprintf(FILE *stream, const char *format, va_list arg)`
  `vsprintf(char *s, const char *format, va_list arg)`
  The same as the functions above, only the argument list is set to `arg`.

Table 8.1: Libc - printf transformations

Next: 8.1.2 Formatted Input Up: 8.1 I/O Function in Previous: 8.1 I/O Function in