printf
printf is used to display information on screen, that is,
standard output. printf is a function that returns the
number of characters printed by the printf
function.
Program
#include <stdio.h>
main()
{
int i = 0;
i=printf("abcde"); // A
printf("total characters printed
%d\n",i); //B
}
Explanation
- Here, five characters are
printed by statement A. So, i will get the value 5.
- Statement B prints the value
of i as 5.
- The general format for the printf statement has a first
string argument followed by any additional arguments.
- In statement B, "total characters
printed %d\n" is
the first string argument.
- i is the second argument. You
may have multiple arguments, but that depends on what value you have to
print. For each additional argument you will have to include a
placeholder. Each placeholder begins with %. In statement B, %d is the placeholder.
- For the second argument i, the placeholder is %d. So when you need an
integer value, you have to use %d. The placeholders are given for each data
type.
- For example, if you want to
print i and j, you may have to use two
placeholders. Any material in the first string argument, other than the
placeholder and characters, represents the escape sequence. In this
example, the escape sequence character is \n, which is not printed but acts as a directive.
For example, the \n directive indicates that
the next printing should be done on a new line.
Points to Remember
- printf is used to direct output to
standard output format.
- printf is a function that returns
the number of characters printed.
Placeholders are used to print values of
arguments supplied in print. The directives in the placeholders control
printing.
Program/Example
The
general form of a placeholder is:
- % flags field-width
precision prefix type-identifier.
Type-identifiers
The
type-identifiers are as follows:
- d, i Signed integers
- o Unsigned integers displayed
in octal form.
- u Unsigned integers in
decimal form.
- x Unsigned integers in
hexadecimal form, and the hexadecimal characters a, b, c, d, e, and f
printed in lowercase.
- X Unsigned integer in
hexadecimal form, and the hexadecimal characters A, B, C, D, E, and F
printed in uppercase.
- c Any value converted to unsigned
char and displayed; c is used mainly for printing
characters.
- s The argument is converted
to a character array and is printed; the last null in the string is not
printed.
- f Floating point.
- e, E Floating point displayed in
exponential form. It will have one digit to the left of the decimal point;
the number of digits on the right side of the decimal point depends on the
required precision.
- g, G The value can be printed in
floating point or exponential form. The exponential form is used if the
exponent is less than –1 or if the exponent causes more places than
required by the specified precision; the decimal point appears only if it
is followed by a digit.
- n This indicates to print the
number of characters that are printed so far by printf.
- p It indicates an additional
argument pointer to void; the value of the pointer is converted to a
sequence of characters.
Type prefixes
- h It can appear before type
indicators d, i, o, u, x, and X. It indicates that the value to be
displayed should be interpreted as short; for example, short integer (hd)
and short unsigned integer (hu).
- l It can appear before
type-identifiers d, i, o, u, x, and X. It indicates that the value to be
displayed should be interpreted as long; for example, long integer (hd)
and long unsigned integer (hu).
- l, L Available for
type-identifiers e, E, f, g, and G. It indicates that a value should be
indicated as long double.
Field-width
- Field-width indicates the least number
of columns that will be allocated to the output. For example, if you write
%4d to i and the value of i is 10, then 4 columns are
allocated for i and 2 blank are added on
left side of value of i. So
the output is bb10. Here, b indicates blank.
- If the value is more than
the specified column, field-width is ignored and the number of columns
used is equal to the number of columns required by the arguments. So if i is 12345 then 5 columns are
used, even if %4d is specified.
- In any circumstance, the
output width is not shortened, because of field-width.
- If you specify * instead of
field-width then you have to specify additional arguments. For example,
- printf ("%*d\n",
5, 20); // A
- printf ("%*d\n",
20, 5); // B
In A, 5 is substituted for * and it indicates
putting the value 20 in 5 columns.
In B, 20 is substituted for * and it indicates
putting the value 5 in 20 columns.
Precision
- Precision indicates the minimum
number of digits printed for type integers d, i, o, u, x, and X. For
example,
- i. printf("%10.4d\n", 35)
- Here 10 is the field-width
and 4 is the precision, so 10 columns are used for the 4-digit output. To
make 35 into 4 digits, two 0s are added to the left side to make it 0035.
To print 0035 in 10 columns, blanks are added to make the output
bbbbbb0035.
- For floating arguments,
precision indicates how many digits are printed after decimal points. If
precision is more than the number of digits on the right side of the
decimal point, 0s are added to the right side.
- If precision indicates too
few digits, then it is ignored and the number of digits are printed as
necessary.
Flags
- Flag characters are used to give
directives for the output. You can use multiple flag characters in any
order.
- The flag characters are as
follows:
- — Indicates that output is
left justified.
o printf("%-10.4d\n", 25)
o printf("%+d\n", -25);
o printf("%+d\n", 25);
- It causes printing as
o -25
o +25
- <space> Indicates a space for
positive values so that positive values and negative values are aligned.
For example,
o printf("% d\n", 25);
o printf("% d", 25);
- It causes printing in the
form of
o b25
o 25
- In the first case, blank is
displayed.
- − # Indicates that the value
should be converted to another form before displaying. For example, for
hexadecimal values you can indicate 0X; for the floating data type, # indicates
that the decimal point should always be included in the output.
- 0 Used with whole and real
numbers, 0 causes 0s to be padded to complete the field width. If the
precision is specified as 0, then this flag is ignored; if the 0 and –
flags are both specified, the 0 flag is ignored.
Escape
sequences are the
special directives used to format printing. For example, \n indicates that the next printing should start from the first column of
the next line. Following are the escape sequences:
- \a Alert
- Produces a beep or flash;
the cursor position is not changed.
- \b Backspace
- Moves the cursor to the last
column of the previous line.
- \f Form feed
- Moves the cursor to start of
next page.
- \n New line
- Moves the cursor to the
first column of the next line.
- \r Carriage Return
- Moves the cursor to the
first column of the current line.
- \t Horizontal Tab
- Moves the cursor to the next
horizontal tab stop on the line.
- \v Vertical Tab
- Moves the cursor to the next
vertical tab stop on the line.
- \\
- Prints \\.
- \"
- Prints "
- %%
- Prints %.
Points to Remember
- printf returns the number of
characters printed; if some error occurs then it returns a negative value.
- Formating of printf can be controlled by using
flags, field-width, etc.