Formatted Output (printf) in details with examples


Formatted Output :

printf() function is used for printing captions and numerical results. It is highly desirable that the outputs are produced in such a way that they are understandable and are in any easy to use form. It is therfore necessary for the programmer to give careful consideration to the appearance and clarity of the output produced by the prograam.

The printf statement provides certain features that can be effectively exploited to control the alignment and spacing of print-outs on the terminals.

The general form of printf statement :

printf("control string", arg1, arg2,..argn);

Control string consists of three types of items :

  • Characters that will be printed on the screen as they appear.
  • Format Specifications that define the output format for display of each time.
  • Escape sequence characters such as \n, \t and \b.

The control string indicates how many arguments follow and what their types are. The arguments arg1, arg2,..argn are the variables whose values are formatted and printed according to the specifications of the control string. The arguments should match in number, order and type with the format specifications.

A simple format specification has the following form :

% w.p type-specifier

Where w is an integer number that specifies the total number of columns for the output value and pi another integer number that specifies the number of digits to the right of the decimal point (of a real number) or the number of characters to be printed from a string. Both w and p are optional.

Different ways to write the output data using printf() :

  1. Outputting Integer Numbers.
  2. Outputting Real Numbers.
  3. Outputting Single Characters.
  4. Outputting Strings.

1. Outputting Integer Numbers :

The format specification for printing an integer number is : % w d
where w specifies the minimum field width for the output.

However, if a number is greater than the specified field width, it will be printed in full, overriding the minimum specification. d specifies that the value to be printed is an integer. The number is written right-justified in the given field width. Leading blanks will appear as necessary.

It is possible to force the printing to be left-justified by placing a minus sign directly after the % character. It is also possible to pad with zeros the leading blanks by placing a 0(zero) before the field width specifier.

the minus (-) and zero (0) are known as flags.

long integers may be printed by specifying Id in the place of d in the format specification. Similarly, we may use hd for printing short integers.

2. Outputting Real Numbers :

The output of a real number may be displayed in decimal notation using the format specification : % w.p f

The integer w indicates the minimum of positions that are to be used for the display of the value and the integer p indicates the number of digits to be displayed after the decimal point(precision). The value, when displayed, is rounded to decimal places and printed right-justified in the field of w columns. Leading blanks and trailing zeros will appear as necessary.

The default precision is 6 decimal places.

we can also display a real number in exponential notation by using the format specification : % w.p e

The value will be rounded off and printed right justified in the field of w columns. Padding the leading blanks with zeros and printing with left-justification are also possible by using flags 0 or - before the field width specifier w.

Some systems also support a special field specification character that lets the user define the field size at run time. This takes the following form :

printf("%*.*f", width, precision, number);

In this case, both the field width and the precision are given as arguments which will supply the values for w and p.

printf("%*.*f", 7, 2, number);

is equivalent to

printf("%7.2f", number);

The advantage of this format is that the values for width and precision may be supplied at run time, thus making the format a dynamic one.

3. Outputting Single Characters :

A single character can be displayed in a desired position using the format : % W c

The character will be displayed right-justified in the field of w columns. We can make the display left-justified by placing a minus sign before the integer w. The default value for w is 1.

4. Outputting String :

The format specification for outputting strings is similar to that of real numbers. It is of the form % w.p s where w specifies the field width for display and p instructs that only the first p characters of the string are to be displayed. The display is right-justified.

0 Komentar untuk "Formatted Output (printf) in details with examples"

Back To Top