printf

Conversion Specifications
Each conversion specification begins with a % and ends with a conversion character. Between the % and the conversion character there may be, in order:

- A minus sign. This tells printf to left-adjust the conversion of the argument.
number

An integer that specifies field width;

printf will print a conversion of ARGUMENT in a field at least number characters wide.

If necessary it will be padded on the left (or right, if left-adjustment is called for) to make up the field width.

. A period, which separates the field width from the precision.
number

An integer, the precision, which specifies the 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.

h or l

These differentiate between a short and a long integer, respectively,

and are generally only needed for computer programming.

The conversion characters themselves, which tell printf what kind of argument to expect, are as follows:

conversion character argument type
d, i An integer, expressed as a decimal number.
o

An integer, expressed as an unsigned octal number.

x, X

An integer, expressed as an unsigned hexadecimal number

u

An integer, expressed as an unsigned decimal number.v

c

An integer, expressed as a character. The integer corresponds to the character'sASCII code.

s

A string.

f

A floating-point number, with a default precision of 6.

e, E

A floating-point number expressed in scientific notation, with a default precision of 6.

p

A memory address pointer.

%

No conversion; a literal percent sign ("%") is printed instead.

 

A width or precision may be represented with an asterisk ("*"); if so, the asterisk reads in an argument, which must be an integer, and uses that value. For example,
printf "%.*s" 5 "abcdefg"
...produces the following output:
abcde
The following table represents the the way that printf would output its ARGUMENT, "computerhope", using various FORMAT strings. Each string is enclosed in quotes so that it's easier to see the exact extent of each:

FORMAT string ARGUMENT string output string
"%s" "computerhope" "computerhope"
"%8s" "computerhope" "computerhope"
"%.8s" "computerhope" "computer"
"%-8s" "computerhope" "computerhope"
"%-15s" "computerhope" "computerhope "
"%15.8s" "computerhope" " computer"
"%-15.8" "computerhope" "computer "
"%-15.2" "computerhope" "co "

来自 <http://www.computerhope.com/unix/uprintf.htm>

posted @ 2016-10-13 13:16  碗里唱歌  阅读(44)  评论(0)    收藏  举报