printf函数是C中的一个输出函数,而格式串(format string)又是它的一项很重要的参数,我们来看看format string的manual解释。

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 specifications, 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 minimum field width, an optional precision and an optional length modifier.

  1. 格式串是字符串。
  2. 格式串由零个或多个directives组合而成:平常字符和conversion specifications。
  3. 每个conversion specification有零个或多个跟随的参数(subsequent arguments)。
  4. conversion specification形如:
    %(flags)(minimum field width)(precision)(length modifier)【conversion specifier】

    flags

    mininum field width

    precision

    length modifer

    conversion specifier

    #

    可选数字串;指定该参数占用的宽度

    .可选数字串;

    hh

    d,i

    0零填充

     

     

    h

    o,u,x,X

    -左对齐

     

     

    l

    e,E

    ‘ ‘

     

     

    ll

    f,F

    +正负号

     

     

    L

    g,G

    ‘(SUSv2)

     

     

    q

    a,A

    I

     

     

    j

    c

     

     

     

    z

     

     

     

    t

    C(SUSv2)

     

     

     

     

    S(SUSv2)

     

     

     

     

    p

     

     

     

     

    n

     

     

     

     

    m

     

     

     

     

    %

这里是个例子,如何输出unsigned long long int类型?
有两种方法:

  1. "%llun”,但此法在msys上不work
  2. "%I64u”
Advertisements