posts - 274,  comments - 1258,  trackbacks - 0

按:寻寻觅觅了许久,终于找到了一个比较详细的版本,来源是sun公司的文档,应该都比较权威。原文见http://docs.sun.com/app/docs/doc/806-7023/6jfu564ls?l=zh_TW&a=view



NAME

    scanf, sscanf- convert formatted input

SYNOPSIS

    $(NUCLEUS_DIR)/lib/embedded/libebd.s.a
    #include <stdio.h>
    int scanf(const char * format, ... );
    int sscanf(const char * s, const char * format, ... );

API RESTRICTIONS

    The function or functions documented here may not be used safely in all application contexts with all APIs provided in the ChorusOS 5.0 product.

    See API(5FEA) for details.

DESCRIPTION

    The scanf() function reads from the standard input channel, which is operating system dependent. The sscanf() function reads from the character string s . Each function reads characters, interprets them according to a format, and stores the results in its arguments. The functions require, as arguments, a control string format described below, and a set of pointer arguments indicating where the converted input should be stored.

    The control string usually contains conversion specifications, which are used to direct interpretation of input sequences. The control string may contain:

    1. White-space characters (blanks, tabs, new-lines, or form-feeds) which, except in two cases described below, cause input to be read up to the next non-white-space character.

    2. An ordinary character (not % ), which must match the next character of the input channel.

    3. Conversion specifications, consisting of the character % , an optional assignment suppression character * , an optional numerical maximum field width, an optional l (ell) or h indicating the size of the receiving variable, and a conversion code.

    The format of the conversion specification has the general form:

    %[*] [width] [{h | l | ll | q | L}]conversion_code

    A conversion specification directs the conversion of the next input field; the result is placed in the variable pointed to by the corresponding argument, unless assignment suppression was indicated using * . The suppression of assignment allows you to define an input field to be ignored. An input field is defined as a string of non-space characters; it extends to the next inappropriate character or until the field width, if specified, is exhausted. For all descriptors except ``['' and ``c'', white space leading an input field is ignored.

    Argument Size

      An optional h , l (ell), q , and ll (ell ell) specifies the expected size of the actual argument for the d , i , o , u , or x conversion codes. They are ignored if specified before any other conversion code.

      Their meanings are:

      h

      The argument is a pointer to a short integer.

      l

      The argument is a pointer to a long integer.

      q

      The argument is a pointer to a quad integer.

      ll

      The argument is a pointer to a long long integer.

      An optional l (ell), or L specifies the expected size of the actual argument for the e , f , or g conversion codes.

      They are ignored if specified before any other conversion code.

      Their meanings are:

      l

      The argument is a pointer to a double.

      L

      The argument is a pointer to a long double.

    Conversion Codes

      The conversion code indicates the interpretation of the input field; the corresponding pointer argument must be of a restricted type. For a suppressed field, no pointer argument is given. The following conversion codes are legal:

      %

      A single % is expected in the input at this point; no assignment is performed.

      d

      A decimal integer is expected; the corresponding argument should be an integer pointer.

      u

      An unsigned decimal integer is expected; the corresponding argument should be an unsigned integer pointer.

      o

      An octal integer is expected; the corresponding argument should be an integer pointer.

      x

      A hexadecimal integer is expected; the corresponding argument should be an integer pointer.

      p

      Same as the x conversion code, except that the corresponding argument must be a pointer to a pointer to be voided.

      i

      An integer is expected; the corresponding argument should be an integer pointer. It will store the value of the next input item interpreted according to C conventions: a leading "0" implies octal; a leading "0x" implies hexadecimal; otherwise, decimal.

      n

      Stores into an integer (whose address is specified in the argument) the total number of characters (including white space) that have been scanned since the function call. No input is consumed.

      e , f , g

      A floating point number is expected; the next field is converted accordingly and stored through the corresponding argument, which should be a pointer to a float . The input format for floating point numbers is an optionally signed string of digits, possibly containing a decimal point, followed by an optional exponent field consisting of an E or an e , followed by an optional +, -, or space, followed by an integer.

      s

      A character string is expected; the corresponding argument should be a character pointer pointing to an array of characters large enough to accept the string and a terminating \\0 , which will be added automatically. The input field is terminated by a white-space character.

      c

      A character is expected; the corresponding argument should be a character pointer. The usual skipping over white space is suppressed in this case; to read the next non-space character, use %1s . If a field width is given, the corresponding argument should refer to a character array; the number of characters indicated is read.

      D,O,X,E,F

      Are semantically equivalent to, respectively, ld , lo , lx , le , lf .

      [

      Indicates string data and the usual skipping over leading white space is suppressed. The left bracket is followed by a set of characters, called the scanset , and a right bracket; the input field is the maximaum sequence of input characters consisting entirely of characters in the scanset. The circumflex ( ^ ), when it appears as the first character in the scanset, serves as a complement operator and redefines the scanset as the set of all characters not contained in the remainder of the scanset string. There are certain conventions used in the construction of the scanset. A range of characters may be represented by the construct first-last , thus [0123456789] may be expressed [0-9]. Using this convention, first must be lexically less than or equal to last , otherwise the dash will stand for itself. The dash will also stand for itself whenever it is the first or the last character in the scanset. To include the right square bracket as an element of the scanset, it must appear as the first character (possibly preceded by a circumflex) of the scanset, and in this case it will not be syntactically interpreted as the closing bracket. The corresponding argument must point to a character array large enough to hold the data field and the terminating \\0 , which will be added automatically. At least one character must match for this conversion to be considered successful.

      The scanf() conversion terminates at EOF, at the end of the control string, or when an input character conflicts with the control string. In the latter case, the offending character is left unread in the input channel.

      Reaching the end of the character string s for sscanf() is equivalent to encountering EOF for scanf() .

      The scanf() function returns the number of successfully matched and assigned input items; this number can be zero in the event of an early conflict between an input character and the control string. If the input ends before the first conflict or conversion, EOF is returned.

EXAMPLES

    The call:


    int i, n; float x; char name[50];
    n = scanf("%d%f%s", &i, &x, name);

    with the input line:


    25 54.32E-1 thompson
                                

    will assign to n the value 3 , to i the value 25 , to x the value 5.432 , and name will contain thompson\\0 .

    Or:


    int i; float x; char name[50];
    (void) scanf("%2d%f%*d %[0-9]", &i, &x, name);

    with input:


    56789 0123 56a72
                                

    will assign 56 to i , 789.0 to x , skip 0123 , and place the string 56\\0 in name . The next call to getchar (3STDC) will return a .

NOTE

    Trailing white space (including a new-line) is left unread unless matched in the control string.

DIAGNOSTICS

    These functions return EOF on end of input and a short count for missing or illegal data items.

ATTRIBUTES

    See attributes(5) for descriptions of the following attributes:

    ATTRIBUTE TYPEATTRIBUTE VALUE
    Interface Stability Evolving

posted on 2007-12-07 01:05 踏雪赤兔 阅读(2008) 评论(1)  编辑 收藏 引用 所属分类: 速查手册玩转编程

FeedBack:
# re: C风格输入格式化字符串语法[转][未登录]
2007-12-14 18:11 | 星星
果然睇吾明~~~~~~~  回复  更多评论
  
只有注册用户登录后才能发表评论。

百度空间| 见闻日记| 编程感悟
我的twitter


LOGO

自我介绍:百度厂基础平台车间的一名挨踢民工。擅长C++、算法、语言设计、分布式计算,也用过Java,Python, PHP,JS/AS等语言开发。请关注我的twitter (免翻墙版) 发QQ消息


添加到收藏夹 Locations of visitors to this page

常用链接

随笔分类(300)

随笔档案(274)

文章分类(38)

相册

收藏夹(54)

与博主互动

博客手拉手

搜索

  •  

积分与排名

  • 积分 - 399486
  • 排名 - 10

最新评论

阅读排行榜

评论排行榜