CMPSC 311, Introduction to Systems Programming

A simple explanation of printf()



This is enough to get you through the Introduction to Unix notes.  We won't do anything complicated here.


printf() does formatted output.  The first argument to printf() is a format control string (also, format string, control string, or format specification), and the remaining arguments are values to be printed, or pointers to values to be printed.  Characters in the format string, and converted values from the remaining arguments, are sent to the standard output stream stdout.

Character constants in C are written as 'a', and string constants (string literals, officially) as "a".  Strings in C are arrays of characters, with the special property that there is a null character ('\0', numerical value 0) marking the end of the string.  If you didn't know or remember that, read CP:AMA Sec. 13.1, or C:ARM Sec. 2.7.4.

When the character \ is encountered in the format string, that starts an escape character sequence; see CP:AMA p. 41-42 and p. 137-138, or C:ARM Sec. 2.7.5-6, for details.  This works for strings and characters in general, and is not specific to printf().  The most common uses are \n (newline, to go to the next line on the output device), \" (to put a double quote in a string literal without ending the string), and \\ (to put a single backslash in a string literal).

When the character % is encountered in the format string, that starts a format code (a conversion specification) that tells what to do with the next argument to printf().  This much is specific to printf() and scanf(), which is used for formatted input.

The two simplest format codes are %d, which means print an integer value in decimal (the next argument should be an int), and %s, which means print a character string by copying the characters from an array (the next argument should be a char *, and it should be aimed at a null-terminated character string).  Use the format code %% to print one % character.

fprintf() allows you to select an output stream different from stdout.  Otherwise it works the same way.  stdout is opened automatically when your program starts to run, but other output streams need to be opened explicitly as part of the program.

It's relatively easy to get printf() to fail or behave strangely if you don't match the format codes and the argument types properly.  Most compilers, and the lint program on Solaris, can report such a problem, if you turn up the warning options.



Here are some examples.  We used the symbol @ to indicate where the next character to be printed will appear, but of course @ won't actually appear on the screen except as the cursor position (probably a blinking black rectangle).

  printf("hello world");
hello world@

  printf("hello world\n");      // \n is the newline character
hello world
@

  char buffer[] = "it's me";
  printf("hello world %s\n", buffer);
hello world it's me
@

  printf("the value is %d\n", 17);
the value is 17
@

  int a = 1, b = 2;
  printf("the sum of %d and %d is %d\n", a, b, a+b);
the sum of 1 and 2 is 3
@


Note the important difference between the first two examples - omitting \n from the end of the format string means that the next character to be output will be on the same line, not on the next line.



Exercises

1.  Explain the difference between C's printf("\n"); and C++'s cout << endl;

2.  Since C++ includes the C Standard Library, you can call printf() from a C++ program.  What happens if you use both printf() and cout << in the same program?

3.  Suppose you want to write formatted output into a character string, instead of sending it to an output stream (which is what printf() and fprintf() do).  What function should you use instead?  What problems might you encounter using this function?



You can find a table of format codes for additional types in CP:AMA, C:ARM, or on the man page.



Last revised, 9 Jan. 2012