16.5: Formatted Output (printf and friends)

C's venerable printf function, which we've been using since day one, prints or writes formatted output to the standard output. As we've seen (by example, if not formally), printf's operation is controlled by its first, ``format'' argument, which is either a simple string to be printed or a string containing percent signs and other characters which cause the formatted values of printf's other arguments to be interspersed with the other text (if any) of the format string.

So far, we've been using simple format specifiers such as %d, %f, and %s. But format specifiers can actually consist of several parts. You can specify a ``field width''; for example, %5d prints an integer in a field five characters wide, padding the integer's value with extra characters if necessary so that at least five characters are printed. You can specify a ``precision''; for example, %.2f formats a floating-point number with two digits printed after the decimal point. You can also add certain characters which specify various options, such as how a too-narrow field is to be padded out to its field width, or what type of number you're printing. For example, %-5d indicates that the padding characters should be added after the field's value (so that it's left-justified), and %ld indicates that you're printing a long instead of a plain int.

Formally, then, the complete framework for a printf format specifier looks like

	% flags width . precision modifier character
where all of the parts except the % and the final character are optional.

The width gives the minimum overall width of the output (the field) generated by this format specifier. If the output (the number of digits or characters) would be less than the width, it will be padded on the right (or left, if the - flag is present), usually with spaces. If the output for the field ends up being larger than the specified width, however, the field essentially overflows or grows; the output is not truncated or anything. That is, printf("%2d", 12345) prints 12345.

The precision is either:

For example, printf("%.3s", "Hello, world!") prints Hel, and printf("%.5d", 12) prints 00012.

Either the width or the precision (or both) can be specified as *, which indicates that the next int argument from the argument list should be used as the field width or precision. For example, printf("%.*f", 2, 76.54321) prints 76.54.

The flags are a few optional characters which modify the conversion in some way. They are:

The modifier specifies the size of the corresponding argument: l for long int, h for short int, L for long double.

Finally, the format character controls the overall appearance of the conversion (and, along with the modifier, specifies the type of the corresponding argument). We've seen many of these already. The complete list of format characters is:

When you want to print to an arbitrary stream, instead of standard output, just use fprintf, which takes a leading FILE * argument:

	fprintf(stderr, "Syntax error on line %d\n", lineno);

Sometimes, it's useful to do some printf-like formatting, but not output the string right away. The sprintf function is a printf variant which ``prints'' to an in-memory string rather than to a FILE * stream. For example, one way to convert an integer to a string (the opposite of atoi) is:

	int i = 123;
	char string[20];
	sprintf(string, "%d", i);
One thing to be careful of with sprintf, though, is that it's up to you to make sure that the destination string is big enough.


Read sequentially: prev next up top

This page by Steve Summit // Copyright 1996-1999 // mail feedback