A standard function in C that outputs formatted text.

The cannonical statement:

printf("Hello world!");

Outputs Hello World (without a newline) to stdout.

The printf() function can do much more than simply output a string constant.
It can also format and insert strings and other variables into the output string by putting format specifiers into the format arg (the first one), then passing the variables you want to substitute for the format specifiers in the order that you want them to be used. %d (%i) is for int's, %c is for char's, %s is for strings (char* or char's), %f is for float's.

Example: printf( "num = %d" , num );
Example: printf( "string = %s", string );
Example: printf( "char 1 is %c and char 2 is %c", char1, char2 );

printf() is declared in stdio.h.

I find it sort of funny that there is also an application that does printf formatting, with GNU sh-utils. Watch:

	$ printf "%s%s %s %i\n" foo bar baz 256
	foobar baz 256
	$

Gee, that was so useless! Especially considering all the command line args are passed as strings. But I guess it could be "useful" for shell scripting.. Oh yeah, and %x..

printf format commands:

Code    Format

%c      Character
%d      Signed decimal integer
%i      Signed decimal integer
%e      Scientific notation with lowercase e
%E      Scientific notation with uppercase E
%f      Decimal floating point
%g      Uses %e or %f, whichever is shorter
%G      Uses %E or %f, whichever is shorter
%o      Unsigned octal
%s      String of characters
%u      Unsigned decimal integer
%x      Unsigned hexadecimal with lowercase letters
%X      Unsigned hexadecimal with uppercase letters
%p      Displays a pointer
%n      The associated argument shall be an integer pointer which receives the number of characters written at this point
%%      Prints a % sign


The other writeups demonstrate some of these. Also check out the C backslash codes for useful printf formatting information. This information is taken from Teach Yourself C, 2nd ed. by Herbert Schildt.

Please realize that your first argument to printf should always be a constant with a format string. If you don't, you are putting incorrect code into your program. Why? Because printf always looks for these format codes in the first argument it is passed. If your first argument is a variable (potentially supplied by the user), then printf will evaluate whatever data is in it for format commands opening you up to potential irregular behavior of the function and possible security breach. This is bad and has been the cause of several security errors.
The function printf provides the formatted output conversion in the C programming language. Its function signature is as follows:
int printf (const char *format, [arg1, arg2,] ...);
printf converts, formats, and prints its arguments on the standard output under the control of format. And it returns the number of characters printed.

The format string contains two kind of objects, 'ordinary or literal characters', which are copied to the output stream as such, and 0 or more 'conversion specifications', each of which is used to convert and print the next successive argument to the printf.

The conversion specifications start with the '%' symbol and end with a conversion symbol. Between these two, there can exist, in order:

  • A minus sign ('-') to specify left adjustment of the converted argument. The default adjustment is 'right', which can also be explicitly specified using a '+' symbol.
  • A number specifying minimum field width. The converted argument will be printed in a field atleast this wide. Padding is used to make up if needed.
  • A period ('.'), to separate the field width from precision.
  • A number, the precision, specifying maximum number of characters to be printed from a string, or the number of digits after the decimal point of a floating point value, or the minimum number of digits for an integer.
  • An 'h' if the integer is to be printed as a short, or 'l' if as a long.

A width or precision may also be specified as *, in which case the value is computed by converting the next argument (must be an integer). For example:

printf("%.*s", max, s);
prints at most max characters from string s.


Thanks to ariels for pointing out a mistake in the signature, it's const char *, not just char *

%n

Last weekend I implemented my own printf() function, sans floating point support. And while reading my copy of Kernighan and Ritchie's ANSI C book to review the specifications, I learned of the %n operator for the first time ever. I had never needed it. Anytime I've ever needed to know the size of things being formatted my needs were always either met by just using the return value of the *printf() function I was using, or by sizing up arguments before allocating a buffer for an snprintf() call.

But when I learned of %n, it was a piece of cake to implement. But now I am having a hard time thinking of a good use for this. Does anybody ever use this conversion character on a regular basis? At all?

Log in or registerto write something here or to contact authors.