To print
- short use
%hi
%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)%s
for a string (char * or char [])- a percent sign, use
%%
Prototypes of
- 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. 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).
Flags can be in any order. Available flags are
-
: 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.
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.
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. Available precision specifiers
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.
Length modifier
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.
Conversion character
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.
Reference :-
Format Conversions: printf, fprintf, sprintf