Home > Electronics Tutorials > C Language Tutorial > stdio.h - Formatted I/O Functions - printf

C Language Programming Library Reference Guide

stdio.h - Formatted I/O Functions - printf

Declarations: int fprintf(FILE *stream, const char *format, ...);
int printf(const char *
format, ...);
int sprintf(char *
str, const char *format, ...);
int vfprintf(FILE *
stream, const char *format, va_list arg);
int vprintf(const char *
format, va_list arg);
int vsprintf(char *
str, const char *format, va_list arg);
 

The ..printf functions provide a means to output formatted information to a stream.

fprintf sends formatted output to a stream
printf sends formatted output to stdout
sprintf sends formatted output to a string
vfprintf sends formatted output to a stream using an argument list
vprintf sends formatted output to stdout using an argument list
vsprintf sends formatted output to a string using an argument list
 

These functions take the format string specified by the format argument and apply each following argument to the format specifiers in the string in a left to right fashion. Each character in the format string is copied to the stream except for conversion characters which specify a format specifier.

The string commands (sprintf and vsprintf) append a null character to the end of the string. This null character is not counted in the character count.

The argument list commands (vfprintf, vprintf, and vsprintf) use an argument list which is prepared by va_start. These commands do not call va_end (the caller must call it).

A conversion specifier begins with the % character. After the % character come the following in this order:

[flags] Control the conversion (optional).
[width] Defines the number of characters to print (optional).
[.precision] Defines the amount of precision to print for a number type (optional).
[modifier] Overrides the size (type) of the argument (optional).
[type] The type of conversion to be applied (required).
 

Flags:
 

- Value is left justified (default is right justified). Overrides the 0 flag.
+ Forces the sign (+ or -) to always be shown. Default is to just show the - sign. Overrides the space flag.
space Causes a positive value to display a space for the sign. Negative values still show the - sign.
# Alternate form:
Conversion Character Result
o Precision is increased to make the first digit a zero.
X or x Nonzero value will have 0x or 0X prefixed to it.
E, e, f, g, or G Result will always have a decimal point.
G or g Trailing zeros will not be removed.
0 For d, i, o, u, x, X, e, E, f, g, and G leading zeros are used to pad the field width instead of spaces. This is useful only with a width specifier. Precision overrides this flag.
 

Width:
The width of the field is specified here with a decimal value. If the value is not large enough to fill the width, then the rest of the field is padded with spaces (unless the 0 flag is specified). If the value overflows the width of the field, then the field is expanded to fit the value. If a * is used in place of the width specifer, then the next argument (which must be an int type) specifies the width of the field. Note: when using the * with the width and/or precision specifier, the width argument comes first, then the precision argument, then the value to be converted.

Precision:
The precision begins with a dot (.) to distinguish itself from the width specifier. The precision can be given as a decimal value or as an asterisk (*). If a * is used, then the next argument (which is an int type) specifies the precision. Note: when using the * with the width and/or precision specifier, the width argument comes first, then the precision argument, then the value to be converted. Precision does not affect the c type.

[.precision] Result
(none) Default precision values:
1 for d, i, o, u, x, X types. The minimum number of digits to appear.
6 for f, e, E types. Specifies the number of digits after the decimal point.
For g or G types all significant digits are shown.
For s type all characters in string are print up to but not including the null character.
. or .0 For d, i, o, u, x, X types the default precis ion value is used unless the value is zero in which case no characters are printed.
For f, e, E types no decimal point character or digits are printed.
For g or G types the precision is assumed to be 1.
 
.n For d, i, o, u, x, X types then at least n digits are printed (padding with zeros if necessary).
For f, e, E types specifies the number of digits after the decimal point.
For g or G types specifies the number of significant digits to print.
For s type specifies the maximum number of characters to print.
 
 

Modifier:
A modifier changes the way a conversion specifier type is interpreted.

[modifier] [type] Effect
h d, i, o, u, x, X Value is first converted to a short int or unsigned short i nt.
h n Specifies that the pointer points to a short int.
l d, i, o, u, x, X Value is first converted to a long int or unsigned long int .
l n Specifies that the pointer points to a long int.
L e, E, f, g, G Value is first converted to a long double.
 

Conversion specifier type:
The conversion specifier specifies what type the argument is to be treated as.

[type] Output
d, i Type signed int.
o Type unsigned int printed in octal.
u Type unsigned int printed in decimal.
x Type unsigned int printed in hexadecimal as dddd using a, b, c, d, e, f.
X Type unsigned int printed in hexadecimal as dddd using A, B, C, D, E, F.
f Type double printed as [-]ddd.ddd.
e, E Type double printed as [-]d.ddde�dd where there is one digit printed before the decimal (zero only if the value is zero). The exponent contains at least two digits. If type is E then the exponent is printed with a capital E.
g, G Type double printed as type e or E if the exponent is less than -4 or greater than or equal to the precision. Otherwise printed as type f. Trailing zeros are removed. Decimal point character appears only if there is a nonzero decimal digit.
c Type char. Single character is printed.
s Type pointer to array. String is printed according to precision (no precision prints entire string).
p Prints the value of a pointer (the memory location it holds).
n The argument must be a pointer to an int. Stores the number of characters printed thus far in the int. No characters are printed.
% A % sign is printed.

The number of characters printed are returned. If an error occurred, -1 is returned.

Note: To report broken links or to submit your projects, tutorials please email to Webmaster

Discover

     more......