Shell Shell

Format Conversions:
printf, fprintf, sprintf

To get started, use %hi to display a short, %i for an int, %li for a long, %G for a float or double, %LG for a long double, %c for a char (or %i to display it as a number), and %s for a string (char * or char []). Then refine the formatting further as desired.

To print a percent sign, use %%.

1. Prototypes ( in <stdio.h>)

int printf(const char *format, ...)

int fprintf(FILE *stream, const char *format, ...)

int sprintf(char *string, const char *format, ...)

The functions return the number of characters written, or a negative value if an error occurred.

2. Format string

The format string is of the form

% [flags] [field_width] [.precision] [length_modifier] conversion_character

where components in brackets [] are optional. The minimum is therefore a % and a conversion character (e.g. %i).

3. Flags

These can be in any order.

Flag Meaning
- The output is left justified in its field, not right justified (the default).
+ Signed numbers will always be printed with a leading sign (+ or -).
space Positive numbers are preceded by a space (negative numbers by a - sign).
0 For numeric conversions, pad with leading zeros to the field width.
# An alternative output form. For o, the first digit will be '0'. For x or X, "0x" or "0X" will be prefixed to a non-zero result. For e, E, f, F, g and G, the output will always have a decimal point; for g and G, trailing zeros will not be removed.

4. Field width

The converted argument will be printed in a field at least this wide, and wider if necessary. If the converted argument has fewer characters than the field width, it will be padded on the left (or right, if left adjustment has been requested) to make up the field width. The padding character is normally ' ' (space), but is '0' if the zero padding flag (0) is present.

If the field width is specified as *, the value is computed from the next argument, which must be an int.

5. Precision

A dot '.' separates the field width from the precision.

If the precision is specified as *, the value is computed from the next argument, which must be an int.

Conversion Meaning
s The maximum number of characters to be printed from the string.
e, E, f The number of digits to be printed after the decimal point.
g, G The number of significant digits.
d, i, o, u, x, X The minimum number of digits to be printed. Leading zeros will be added to make up the field width.

6. Length modifier

Character Meaning
h The value is to be displayed as a short or unsigned short.
l For d, i, o, u, x or X conversions: the argument is a long, not an int.
L For e, f, g or G conversions: the argument is a long double.

7. Conversion character

Character Meaning
d, i Display an int in signed decimal notation.
o Display an int in unsigned octal notation (without a leading 0).
u Display an int in unsigned decimal notation.
x, X Display an int in unsigned hexadecimal notation (without a leading 0x or 0X). x gives lower case output, X upper case.
c Display a single char (after conversion to unsigned int).
e, E Display a double or float (after conversion to double) in scientific notation. e gives lower case output, E upper case.
f Display a double or float (after conversion to double) in decimal notation.
g, G g is either e or f, chosen automatically depending on the size of the value and the precision specified. G is similar, but is either E or f.
n Nothing is displayed. The corresponding argument must be a pointer to an int variable. The number of characters converted so far is assigned to this variable.
s Display a string. The argument is a pointer to char. Characters are displayed until a '\0' is encountered, or until the number of characters indicated by the precision have been displayed. (The terminating '\0' is not output.)
p Display a pointer (to any type). The representation is implementation dependent.
% Display the % character.
David C. Hamill