Skip to the content of the web site.

Formatted printing and scanning

In C++, you may be familiar with streams:

#include <iostream>

int main() {
	string name = "Douglas";
	int age = 42;
	std::cout << "Hello " << name << ", you are " << age << " years old." << std::endl;
	return 0;
}

This allows an object-oriented approach to printing, as a class can provide instruction as to how it can be printed. For example,

#include <iostream>
#include <complex>

int main() {
        std::complex<double> z( 12.34, -56.78 );

        std::cout << "The complex number is " << z << "." << std::endl;

        return 0;
}

has the output

The complex number is (12.34,-56.78).

In C and many other programming languages, a different approach is used to printing primitive datatype including strings, integers, floating-point numbers, characters, or pointers. Instead of printing each individually, as is done in C++, a format string is passed to a function such as printf. By default, the string is printed as is. For example,

	printf( "Hello world!\n" );

will print Hello world! (big surprise, eh?). If you want to include a value stored in a variable, however, you add a placeholder and each placeholder begins with a %. The easiest is to replicate the example above:

#include <stdio.h>

int main( void ) {
	char *name = "Douglas";
	int age = 42;
	printf( "Hello %s, you are %d years old.\n", name, age );

	return 0;
}

The first placeholder %s is replaced by the first argument following the format string, and the second placeholder %d is replaced by the second argument following the formatted string. Some placeholders, or specifiers are listed in the following list of specifiers and their associated types:

%d
int
%u
unsigned int
%f
float or double
%c
char
%s
char * (a null-character–terminated string)
%%
prints a %

A comprehensive list of specifiers, please see printf at cplusplus.com (the library stdio.h is available in C++ as cstdio.)

Note that the order of the additional arguments must match the order in which the specifiers appear. For example,

	int m = 14;
	int n = 14;
	printf( "%d + %d = %d\n", m, n, m + n );

Similarly, one may have something like:

	printf( "Name:                  %s\nUW Student ID Number:  %d\n"
	        "UW User ID:            %s\nAge:                   %d\n"
	        "Cumulative average:    %f\n",
	        "Douglas Harder", 20123456, "dw99hard", 42, 59.6 );

which will print to standard output:

Name:                  Douglas Harder
UW Student ID Number:  20123456
UW User ID:            dw99hard
Age:                   42
Cumulative average:    59.6

This is, of course, a different Douglas Harder from this author. :-)

Note: The C and C++ compilers will concatenate consecutive strings.

Note: If you are exceptionally interested as to how it can print both doubles and floats (eight bytes and four bytes) with the same placeholder, see stackoverflow.com.

Other similar functions include:

fprintf
Write a formatted string to a file
scanf
Scan input for the given format
sscanf
Scan a string for the given format
fscanf
Scan input read from a file for the given format
fprintf
Print to a file
fprintf, scanf scanning input,