Using the output function printf

When you are performing calculations, you usually want to report your results ("the answer") to the screen. printf is a function from the standard input and output library that allows you to display messages on the computer screen, and this library is installed on every computer with a C compiler. To make the standard input and output library available to your program, the following line must appear at the top of your program:

#include <stdio.h>

The general format for the printf function is

printf(control string, other arguments);

When the program is executed, the control string will be displayed exactly as it appears in the program with two exceptions. First, the computer will replace each conversion specification with a value given in the other arguments part of the printf statement. Second, escape sequences will be replaced with special non-printing and hard-to-print characters. It is important to remember that both conversion specifications and escape sequences appear inside the control string!

The first type of replacement, conversion specifications, are "fill-in the blank" style placeholders in the control string. A conversion specification begins with a % and ends with a conversion character. The kind of conversion character you choose is related to the type of what you would like to display on the screen. The different conversion characters are listed in the table below. (This table is also found in your text, A Book on C, page 494.)

Conversion Character
How the corresponding argument is printed
cas a character
das a decimal integer
eas a floating point number; example 7.123000e+00
fas a floating point number; example 7.123000
gin the e-format or f-format, whichever is shorter
sas a string

As an example, consider the following code:

#include <stdio.h>

int main(void)
  int   i=97;
  float x=3.14;

  printf("My favorite numbers are %d and %f.\n",i,x);

  return 0;

In this example, we want to print a value of type int (stored in the variable i). We should choose the conversion specification %d, since "d" is the conversion character for decimal integers. The second value is type float, so we want to use the conversion specification %f, since "f" is the conversion character for floating point numbers. After we compile and run, here is sample output from this code:

My favorite numbers are 97 and 3.140000.

As we discussed in class, characters are "secretly" stored as integers, so what happens if we replace the above printf statement with the following line?

printf("My favorite numbers are %c and %f.\n",i,x);

We have replaced the conversion character d (which displays integers) with the conversion character c (which displays characters). Next, we compile, then run, and here's what we see:

My favorite numbers are a and 3.140000.

Although computers are not very smart (is your favorite number the character 'a'?), they are obedient! We asked the computer to display the character 97 on the screen, and if you check the ASCII table on page 703 of A Book on C, you will find that the ASCII code for the character 'a' is indeed 97. So, the conversion character dictates how printf interprets what is listed in the other arguments.

Check section 11.1 of A Book on C for more details regarding printf conversion characters. Note that there is a typo in the example on pages 494 and 495. (Kelley and Pohl get 'minus one' on their next homework. I bet you can find the error!) This section of your book describes conversion specifications in greater depth, including printf capabilities like right and left justification, displaying a floating point number to a specified number of decimal places, and other topics.

The second type of replacement, escape sequences, are used to display non-printing and hard-to-print characters. In general, these characters control how text is positioned on the screen, for example, newlines and tabs. The problem with some of these characters is that when you type them in your program, it moves the cursor in your editor when what you really want to do is modify the layout of the text displayed when the program is executed. Here is a table that contains several common escape sequences and the corresponding special characters:
Special Character
Escape Sequence
alert (beep)\a
carriage return\r
double quote\"
horizontal tab\t
null character\0
single quote\'
vertical tab\v
question mark\?

Adding the escape sequences to the control string will lead to a different result. Consider the original code with a different control string in the printf statement:

printf("My\tfavorite\nnumbers are \t%d and \t%f.\n",i,x);
The fact that the escape sequences and some of the words in the control string are joined together (like \t and the word favorite) may look awkward, but this produces the same text with added horizontal tabs and newlines:

My      favorite
numbers are     2 and   3.140000.
There are additional examples in A Book on C on pages 6-9, 18-21, and 493-498.
last modified: