Node:Control Letters, Next:, Previous:Basic Printf, Up:Printf



Format-Control Letters

A format specifier starts with the character % and ends with a format-control letter--it tells the printf statement how to output one item. The format-control letter specifies what kind of value to print. The rest of the format specifier is made up of optional modifiers that control how to print the value, such as the field width. Here is a list of the format-control letters:

%c
This prints a number as an ASCII character; thus, printf "%c", 65 outputs the letter A. (The output for a string value is the first character of the string.)
%d, %i
These are equivalent; they both print a decimal integer. (The %i specification is for compatibility with ISO C.)
%e, %E
These print a number in scientific (exponential) notation; for example:
printf "%4.3e\n", 1950

prints 1.950e+03, with a total of four significant figures, three of which follow the decimal point. (The 4.3 represents two modifiers, discussed in the next subsection.) %E uses E instead of e in the output.

%f
This prints a number in floating-point notation. For example:
printf "%4.3f", 1950

prints 1950.000, with a total of four significant figures, three of which follow the decimal point. (The 4.3 represents two modifiers, discussed in the next subsection.)

%g, %G
These print a number in either scientific notation or in floating-point notation, whichever uses fewer characters; if the result is printed in scientific notation, %G uses E instead of e.
%o
This prints an unsigned octal integer.
%s
This prints a string.
%u
This prints an unsigned decimal integer. (This format is of marginal use, because all numbers in awk are floating-point; it is provided primarily for compatibility with C.)
%x, %X
These print an unsigned hexadecimal integer; %X uses the letters A through F instead of a through f.
%%
This isn't a format-control letter, but it does have meaning--the sequence %% outputs one %; it does not consume an argument and it ignores any modifiers.

Note: When using the integer format-control letters for values that are outside the range of a C long integer, gawk switches to the %g format specifier. Other versions of awk may print invalid values or do something else entirely. (d.c.)