文章随机晒最新文章关照最多的

jiayi Rss

printf 星号(*)

| Posted in C/C++ |

1

Today encountered asterisk (*) when refering the printf() function. I met with it long long ago, but what it is has gone away from my mind absolutely…What the man said about the asterisk (*) is as the follows:

The format of printf() is like this

%[flags][fldwidth][precision][lenmodifier]convtype

   Format of the format string
       The  format string is a character string, beginning and ending in its initial shift state, if any.  The format string is composed
       of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifi-
       cations, each of which results in fetching zero or more subsequent arguments.  Each conversion specification is introduced by the
       character %, and ends with a conversion specifier.  In between there may be (in this order) zero or more flags, an optional mini-
       mum field width, an optional precision and an optional length modifier.

       The  arguments must correspond properly (after type promotion) with the conversion specifier.  By default, the arguments are used
       in the order given, where each `* ‘ and each conversion specifier asks for the next argument (and it is an error if insufficiently
       many arguments are given).  One can also specify explicitly which argument is taken, at each place where an argument is required,
       by writing `%m$’ instead of `%’ and `*m$’ instead of `*, where the decimal integer m denotes the position in the  argument  list
       of the desired argument, indexed starting from 1.  Thus,

           printf("%*d", width, num);  # width is passed to ‘*’ as the [fldwidth] of the argument num

       and

           printf("%2$*1$d", width, num);

       are  equivalent.   The second style allows repeated references to the same argument.  The C99 standard does not include the style
       using `$’, which comes from the Single Unix Specification.  If the style using `$’ is used, it must be used  throughout  for  all
       conversions  taking an argument and all width and precision arguments, but it may be mixed with `%%’ formats which do not consume
       an argument.  There may be no gaps in the numbers of arguments specified using `$’; for example, if arguments 1 and 3 are  speci-
       fied, argument 2 must also be specified somewhere in the format string.

Then I will write down the flags associated with printf() as my memo

The flags component of a conversion specification

Flag

Description

-

left-justify the output in the field

+

always display sign of a signed conversion

(space)

prefix by a space if no sign is generated

#

convert using alternate form (include 0x prefix for hex format, for example)

0

prefix with leading zeros instead of padding with spaces

The length modifier component of a conversion specification

Length modifier

Description

hh

signed or unsigned char

h

signed or unsigned short

l

signed or unsigned long or wide character

ll

signed or unsigned long long

j

intmax_t or uintmax_t

z

size_t

t

ptrdiff_t

L

long double

The conversion type component of a conversion specification

Conversion type

Description

d,i

signed decimal

o

unsigned octal

u

unsigned decimal

x,X

unsigned hexadecimal

f,F

double floating-point number

e,E

double floating-point number in exponential format

g,G

interpreted as f, F, e, or E, depending on value converted

a,A

double floating-point number in hexadecimal exponential format

c

character (with l length modifier, wide character)

s

string (with l length modifier, wide character string)

p

pointer to a void

n

pointer to a signed integer into which is written the number of characters written so far

%

a % character

C

wide character (an XSI extension, equivalent to lc)

S

wide character string (an XSI extension, equivalent to ls)

Comments (1)

又强奸我的眼睛 、、、

Write a comment

You must be logged in to post a comment.