5.9.2.1. Stdio: Writing to the Standard Output

The stdio library is a popular one for writing information. It can be used in both C and C++ programs. To use it in a C++ program, #include <cstdio>.

Use function printf ("print formatted"), which takes a format string followed by zero or more values to be inserted into the format.

The format can contain value designators, which start with %, and printf substitutes the n-th value parameter 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" to the standard output.

Value designators include the following.

%i The value has type int. Show it as a (decimal) integer. (%i is the same as %d. The d stands for decimal.)
%5i Like %i, 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.) Number 5 in this example is called a field width.

All of the value designators allow a field width.

%−5i Like %5i, but put the spaces on the right side instead of on the left side.
%li Like %i, but the value has type long. Notice that this is %li, with a lower-case l.
%c The value has type char. Show it as a single character.
%f
%10.2f
Show a number of type float. %10.1f shows the number using a total of at least 10 characters, with 2 digits shown to the right of the decimal point.
%lf
%10.2lf
Similar to %f, but the value has type double. The lower-case l stands for 'long', since type double is sometimes referred to as 'long float'.
%10.2e
%10.2le
Like %f or %lf, but use E format, showing the value using scientific notation. The lower-case l stands for 'long'.
%s The value is a null-terminated string. Show it as a string.
%p The value is a pointer. Show it as a memory address in base 16, prefixed by 0x. A NULL pointer is shown as 0x0 or (null).
%*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 using at least n total characters.

You can use this with any of the field designators, such as c, s and f.


putchar

Statement

  putchar(c);
is equivalent to
  printf("%c", c);
but putchar is much more efficient.


Examples

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
Be sure not to forget to add \n if you want to end the line. Statements
  printf("I am ");
  printf("anxious to finish\n");
writes
  I am anxious to finish
Also do not forget spaces. Statement
  printf("%i%i\n", 25, 32);
writes
  2532


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 an end-of-line at the end of it. Answer

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

  3. Write a definition of procedure writeSpaces(n), which writes n spaces. Assume that n is at least 1. Answer