6D. Writing to the Standard Output


Printf

The <cstdio> library is a popular one for writing information. To use it, include <cstdio>.

To print string s, use printf(s);. For example, statement

  printf("Have a nice day.\n");
writes Have a nice day on stdout. Notice character sequence \n in the string. It is an end-of-line indicator, also called newline. A line only ends when you explicitly end it. For example,
  printf("Have a ");
  printf("nice day.\n");
writes
Have a nice day.
Don't forget to end lines, or your output will be unreadable.


Extra parameters

Printf can actually take any number of parameters. Printf is short for "print formatted"; its first parameter is called a format. The format can contain value designators, each starting with %. Printf substitutes the value of the n-th parameter after the format for the n-th value designator. For example, suppose that x is 25 and y is 500. Then

  printf("x = %i and y = %i\n", x, y)
writes string "x = 25 and y = 500\n" on the standard output.


Value designators

Value designators include the following.

%i
%d

The value has type int. Show it as a (decimal) integer, as opposed to some other number base. (%i is the same as %d. The d stands for decimal.)

%5i
%5d

These are like %i and %d, but use at least 5 characters by adding spaces on the left-hand side as necessary. (If the number needs more than 5 characters then it will not be shortened.)

For example,

  printf("%6i%4i\n", 45, 300);
  printf("%6i%4i\n", 25000, 2);
  printf("%6i%4i\n", 500, 27);
writes
    45 300
 25000   2
   500  27

Number 5 in %5i is called a field width. All of the value designators allow a field width.

Designator %0d is equivalent to %d.


%-5i

Like %5i, but put the spaces on the right side instead of on the left side. All value designators allow a negative field width.

%li

Like %i, but the value has type long. Notice that this is %ℓi, with a lower-case ell.

%lf
%10.2lf

Show a number of type double. %10.2lf shows the number using a total of at least 10 characters, with 2 digits shown to the right of the decimal point. For example, if variable stddev has value 450.346, then
  printf("The standard deviation is %10.2lf\n", stddev)
prints
The standard deviation is     450.35.
Large numbers will not be cut off to force them to fit into the designated field width. So
  printf("The standard deviation is %0.2lf\n", stddev
prints
The standard deviation is 450.35.
with no padding.

In %lf, the lower-case ℓ stands for long, since type double is sometimes referred to as long float.


%10.2le

Like %lf, but use E format, showing the value using scientific notation. Use this for showing very large or very small values.

%*i

If the field width is given by an asterisk, the width is taken from a parameter. So this actually uses two of the parameters after the format, the field width then the integer to show. For example,
  printf("%*i", n, x);
shows the value of integer x with a field width of n.

%s

Use %s to show a string.

%c

Use %c to show a character.


Example

Statement

  printf("My sister is named %s and she is %i years old\n", "Laura", 10);
writes
 My sister is named Laura and she is 10 years old
Do not forget spaces. Statement
  printf("%i%i\n", 25, 32);
writes
  2532


Putchar

Statement

  putchar(c);
writes character c on the standard output. For example,
  putchar('R');
writes an upper-case R. Putchar writes one character, not a whole string.


Standards

See the following standards for output.


Exercises

  1. Suppose that variable mass  is an integer (type int). Write a printf statement that writes

    This thing's mass is ... kilograms
    
    where the ... has been replaced by the value of mass. Write a newline at the end of it. Answer

  2. Repeat the previous question, but this time assume that variable mass  has type double. Answer