`
xpp02
  • 浏览: 1015306 次
社区版块
存档分类
最新评论

scanf 用法大全

 
阅读更多
关于标准库函数scanf
论坛上很多人对scanf的不太了解,导致程序出错,我想把scanf的具体用法贴出来,希望大家可以共同进步,有什么不对的地方可以提出来。
int scanf(char *format,...);
这应该是scanf的标准形式。先说说关于他的返回值的问题。
库函数几乎都是有返回值的,有些人可能很奇怪,怎么很少人用过scanf的返回值呢?
scanf会返回成功接收到的变量数量的值。比如scanf("%d",&j"),与scanf("%d=",&j),如果接受成功的话返回值都是1
我用如下语句作了测试
#include <stdio.h>
int main (){
int j;
printf ("%d",scanf("%d\n",&j));
return 0;
}
如果你开始就输入回车,程序会继续等待你输入,因为在输入数字的时候,scanf会跳过空白字符。(the c programming language 上说,scanf实际上是用getchar()接受由数字组成的字符串,再转换成数字)
如果我输入ctrl-z(unix上是ctrl-d)则会返回-1(随编译器而定).这实际上就是常量EOF的值,也就是所谓的返回eof
如果我键入的不是数字返回值就是0。但是如果我输入浮点数,又会怎么样呢?
我举的例子中同样会返回1,但是缓冲区会留下垃圾,如果是scanf("%d%d",&a,&b);则会出错。
这是可以使用一个库函数fflush(stdin)来清除缓冲。不过貌似雨中飞燕大姐说这个用法是非标准的。K&R,只是说行为没有定义,但我们可以使用while((c=getchar())!='\n'&&c!=EOF);同样可以清除后面的垃圾
scanf的格式匹配还是比较简单,一定要记住的就是普通变量一定要加上&,否则编译器无法检测错误,但运行肯定会段错误。
┏━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ 代码 │ 意义 ┃
┠────┼────────────────────────────┨
┃%a│读浮点值(仅适用于 C99) ┃
┃%A│读浮点值(仅适用于 C99) ┃
┃%c│读单字符┃
┃%d│读十进制整数┃
┃%i│读十进制、八进制、十六进制整数┃
┃%e│读浮点数┃
┃%E│读浮点数┃
┃%f│读浮点数┃
┃%F│读浮点数(仅适用于 C99) ┃
┃%g│读浮点数┃
┃%G│读浮点数┃
┃%o│读八进制数 ┃
┃%s│读字符串┃
┃%x│读十六进制数┃
┃%X│读十六进制数┃
┃%p│读指针值┃
┃%n│至此已读入值的等价字符数┃
┃%u│读无符号十进制整数┃
┃%[ ]│扫描字符集合┃
┃%%│读 % 符号(百分号)┃
┗━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
前面都很简单,%p,%n很少用到,跳过。要输入%必须要在前面再加一个%,
重点来谈谈%s和%[]。%s是读入一个数组,他与gets的区别就在于%s会以任何的空字符结束,而gets是回车结束。
同样%s前可以加数字,表示只读多少个。
ANSI C 标准向 scanf() 增加了一种新特性,称为扫描集(scanset)。 扫描集定义一个字符集合,可由 scanf() 读入其中允许的字符并赋给对应字符数组。 扫描集合由一对方括号中的一串字符定义,左方括号前必须缀以百分号。 例如,以下的扫描集使 scanf() 读入字符 A、B 和 C:
%[ABC]
使用扫描集时,scanf() 连续吃进集合中的字符并放入对应的字符数组,直到发现不在集合中的字符为止(即扫描集仅读匹配的字符)。返回时,数组中放置以 null 结尾、由读入字符组成的字符串。
对于许多实现来说,用连字符可以说明一个范围。 例如,以下扫描集使 scanf() 接受字母 A 到 Z:
%[A-Z]
重要的是要注意扫描集是区分大小写的。因此,希望扫描大、小写字符时,应该分别说明大、小写字母。
对于%[]还可以用^+任意字符(包括eof)来结束字符串的输入。比如%[^EOF]就是直到有EOF输入,字符串才中止。
但一定要记住就是c语言是缓冲输入,即使你%[^a],再你输入回车之前输入多少的a都是不可能结束的。
%s的输入会跳过空白字符,但是%c则不会。
这也就是
scanf("%d",&h);
scanf("%c",&c);
如果这写的话,变量c放的一定是回车。
如果想实现这种输入,可以在两个语句之间加入一个getchar(),他可以吃掉这个回车,
也可用scanf("%d %c",&h,&c);来做,再输入数字后加一个空格。就可以了
但千万别用scanf("%d\n",&h)!!!!!!!!k&r说的十分清楚,任何非格式化的字符都需要完全匹配。
意味着,只有输入数字后面再加\n才是合法的。

还有就是*加在任何项的前面表示该项不符值,别的就没什么好说的了


Read formatted data from the standard input stream.

int scanf(
const char* format [,argument]...
);

int wscanf(
const wchar_t* format [,argument]...
);
Parameters
format
Format control string.
argument
Optional arguments.
Return Values
Both scanf and wscanf return the number of fields converted and assigned; the return value does not include fields that were read but not assigned.

A return value of 0 indicates that no fields were assigned.

The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character.

Remarks
The scanf function reads data from the standard input stream stdin and writes the data into the location given by argument.

Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format.

If copying takes place between strings that overlap, the behavior is undefined.

The information here applies to the entire scanf family of functions, including the secure versions and describes the symbols used to tell the scanf functions how to parse the input stream, such as the input stream stdin for scanf, into values that are inserted into program variables.

A format specification has the following form:

% [width] [{h | l | ll | I64 | L}]type

The format argument specifies the interpretation of the input and can contain one or more of the following:

White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.

Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in the input stream does not match, scanf terminates.

Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list.

The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in the input stream; the matching characters in the input stream are scanned but not stored. If a character in the input stream conflicts with the format specification, scanf terminates, and the character is left in the input stream as if it had not been read.

When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string.

An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification.

Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number.

The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%.

An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.


function

scanf

<cstdio>
int  scanf ( const char * format, ... );
Read formatted data from stdin
Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format tag within the format string.

Parameters

format
C string that contains one or more of the following items:
  • Whitespace character: the function will read and ignore any whitespace characters (this includes blank spaces and the newline and tab characters) which are encountered before the next non-whitespace character. This includes any quantity of whitespace characters, or none.
  • Non-whitespace character, except percentage signs (%): Any character that is not either a whitespace character (blank, newline or tab) or part of a format specifier (which begin with a% character) causes the function to read the next character from stdin, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character offormat. If the character does not match, the function fails, returning and leaving subsequent characters ofstdin unread.
  • Format specifiers: A sequence formed by an initial percentage sign (%) indicates a format specifier, which is used to specify the type and format of the data to be retrieved fromstdin and stored in the locations pointed by the additional arguments. A format specifier follows this prototype:

    %[*][width][modifiers]type

    where:

    * An optional starting asterisk indicates that the data is to be retrieved fromstdin but ignored, i.e. it is not stored in the corresponding argument.
    width Specifies the maximum number of characters to be read in the current reading operation
    modifiers Specifies a size different from int (in the case of d, i and n), unsigned int (in the case of o, u andx) or float (in the case of e, f and g) for the data pointed by the corresponding additional argument:
    h : short int (for d, i and n), orunsigned short int (for o, u and x)
    l : long int (for d, i and n), orunsigned long int (for o, u and x), or double (fore, f and g)
    L : long double (for e, f and g)
    type A character specifying the type of data to be read and how it is expected to be read. See next table.

    scanf type specifiers: type Qualifying Input Type of argument
    c Single character: Reads the next character. If a width different from 1 is specified, the function readswidth characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end. char *
    d Decimal integer: Number optionally preceded with a + or- sign. int *
    e,E,f,g,G Floating point: Decimal number containing a decimal point, optionally preceded by a+ or - sign and optionally folowed by the e or E character and a decimal number. Two examples of valid entries are -732.103 and 7.12e4 float *
    o Octal integer. int *
    s String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab). char *
    u Unsigned decimal integer. unsigned int *
    x,X Hexadecimal integer. int *
additional arguments
The function expects a sequence of references as additional arguments, each one pointing to an object of the type specified by their corresponding%-tag within the format string, in the same order.
For each format specifier in the format string that retrieves data, an additional argument should be specified.
These arguments are expected to be references (pointers): if you want to store the result of afscanf operation on a regular variable you should precede its identifier with thereference operator, i.e. an ampersand sign (&), like in:
int n;
scanf ("%d",&n);

Return Value

On success, the function returns the number of items successfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens.
In the case of an input failure before any data could be successfully read, EOF is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* scanf example */
#include <stdio.h>

int main ()
{
  char str [80];
  int i;

  printf ("Enter your family name: ");
  scanf ("%s",str);  
  printf ("Enter your age: ");
  scanf ("%d",&i);
  printf ("Mr. %s , %d years old.\n",str,i);
  printf ("Enter a hexadecimal number: ");
  scanf ("%x",&i);
  printf ("You have entered %#x (%d).\n",i,i);
  
  return 0;
}


This example demonstrates some of the types that can be read with scanf:
Enter your family name: Soulie
Enter your age: 29
Mr. Soulie , 29 years old.
Enter a hexadecimal number: ff
You have entered 0xff (255).


scanf函数,与printf函数一样,都被定义在stdio.h里,因此在使用scanf函数时要加上#include<stdio.h>。它是格式输入函数,即按用户指定的格式从键盘上把数据输入到指定的变量之中,其关键字最末一个字母f即为“格式”(format)之意。
[编辑本段]scanf函数的一般形式
  scanf(格式控制,地址表列)
  int scanf(char *format[,argument,...]);
  “格式控制”的含义同printf函数;“地址表列”是由若干个地址组成的表列,可以是变量的地址,或字符串首地址。
  scanf()函数返回成功赋值的数据项数,出错时则返回EOF。
  例:使用scanf函数输入数据。
  #include<stdio.h>
  void main()
  {
  int a,b,c;
  printf("input a,b,c/n");
  scanf("%d%d%d",&a,&b,&c);
  printf("a=%d,b=%d,c=%d",a,b,c);
  }
格式字符说明
  %a,%A 读入一个浮点值(仅C99有效)
  %c 读入一个字符
  %d 读入十进制整数
  %i 读入十进制,八进制,十六进制整数
  %o 读入八进制整数
  %x,%X 读入十六进制整数
  %c 读入一个字符
  %s 读入一个字符串,遇空格、制表符或换行符结束。
  %f,%F,%e,%E,%g,%G 用来输入实数,可以用小数形式或指数形式输入。
  %p 读入一个指针
  %u 读入一个无符号十进制整数
  %n 至此已读入值的等价字符数
  %[] 扫描字符集合
  %% 读%符号
  
  附加格式说明字符表修饰符说明
  L/l 长度修饰符 输入"长"数据
  h 长度修饰符 输入"短"数据
  W 整型常数 指定输入数据所占宽度
  * 表示本输入项在读入后不赋值给相应的变量
scanf的返回值
  scanf的返回值有后面的参数决定
  scanf("%d%d", &a, &b);
  如果a和b都被成功读入,那么scanf的返回值就是2
  如果只有a被成功读入,返回值为1
  如果a和b都未被成功读入,返回值为0
  如果遇到错误或遇到end of file,返回值为EOF。
  且返回值为int型.
使用scanf函数时应该注意的问题
  1、sacnf()中的变量必须使用地址。 
  2、scanf()的格式控制串可以使用其它非空白字符,但在输入时必须输入这些字符。
  3、在用"%c"输入时,空格和“转义字符”均作为有效字符。
  问题一:scanf()函数不能正确接受有空格的字符串?如: I love you!
  #include <stdio.h>
  int main()
  {
  char str[80];
  scanf("%s",str);
  printf("%s",str);
  return 0;
  }
  输入:I love you!
  输出:scanf()函数接收输入数据时,遇以下情况结束一个数据的输入:(不是结束该scanf函数,scanf函数仅在每一个数据域均有数据,并按回车后结束)。
  ① 遇空格、“回车”、“跳格”键。
  ② 遇宽度结束。
  ③ 遇非法输入。
  所以,上述程序并不能达到预期目的,scanf()扫描到"I"后面的空格就认为对str的赋值结束,并忽略后面的"love you!".这里要注意是"love you!"还在键盘缓冲区(关于这个问题,网上我所见的说法都是如此,但是,我经过调试发现,其实这时缓冲区字符串首尾指针已经相等了,也就是说缓冲区清空了,scanf()函数应该只是扫描stdin流,这个残存信息是在stdin中)。我们改动一下上面的程序来验证一下:
  #include <stdio.h>
  int main()
  {
  char str[80];
  char str1[80];
  char str2[80];
  scanf("%s",str);/*此处输入:I love you! */
  printf("%s",str);
  sleep(5);/*这里等待5秒,告诉你程序运行到什么地方*/
  scanf("%s",str1);/*这两句无需你再输入,是对键盘盘缓冲区再扫描 */
  scanf("%s",str2);/*这两句无需你再输入,是对键盘盘缓冲区再扫描 */
  printf("/n%s",str1);
  printf("/n%s",str2);
  return 0;
  }
  输入:I love you!
  输出:
  I
  love
  you!
  好了,原因知道了,那么scanf()函数能不能完成这个任务?回答是:能!别忘了scanf()函数还有一个 %[] 格式控制符(如果对%[]不了解的请查看本文的上篇),请看下面的程序:
  #include "stdio.h"
  int main()
  {
  char string[50];/*scanf("%s",string);不能接收空格符*/
  scanf("%[^/n]",string);
  printf("%s/n",string);
  return 0;
  }
  问题二:键盘缓冲区残余信息问题
  #include <stdio.h>
  int main()
  {
  int a;
  char c; do
  {
  scanf("%d",&a);
  scanf("%c",&c);
  printf("a=%d c=%c/n",a,c);/*printf("c=%d/n",c);*/
  }while(c!='N');
  }
  scanf("%c",&c);这句不能正常接收字符,什么原因呢?我们用printf("c=%d/n",c);将C用int表示出来,启用printf("c=%d/n",c);这一句,看看scanf()函数赋给C到底是什么,结果是c=10 ,ASCII值为10是什么?换行即/n.对了,我们每击打一下"Enter"键,向键盘缓冲区发去一个“回车”(/r),一个“换行"(/n),在这里/r被scanf()函数处理掉了(姑且这么认为吧^_^),而/n被scanf()函数“错误”地赋给了c.解决办法:可以在两个scanf()函数之后加个fflush(stdin);,还有加getch() , getchar()也可以,但是要视具体scanf()语句加那个,这里就不分析了,读者自己去摸索吧。但是加fflush(stdin);不管什么情况都可行。
  (
  函数名: fflush
  功 能: 清除一个流
  用 法: int fflush(FILE *stream);
  )
  #include <stdio.h>
  int main()
  {
  int a;
  char c; do
  {
  scanf("%d",&a);
  fflush(stdin);
  scanf("%c",&c);
  fflush(stdin);
  printf("a=%d c=%c/n",a,c); }while(c!='N');
  }
  这里再给一个用“空格符”来处理缓冲区残余信息的示例:运行出错的程序:
  #include <stdio.h>
  int main()
  {
  int i;
  char j;
  for(i = 0;i < 10;i++)
  {
  scanf("%c",&j);/*这里%前没有空格*/
  }
  }
  使用了空格控制符后:
  #include <stdio.h>
  int main()
  {
  int i;
  char j;
  for(i = 0;i < 10;i++)
  {
  scanf(" %c",&j);/*注意这里%前有个空格*/
  }
  }
  可以运行看看两个程序有什么不同。
  问题三: 如何处理scanf()函数误输入造成程序死锁或出错?
  #include <stdio.h>
  int main()
  {
  int a,b,c; /*计算a+b*/
  scanf("%d,%d",&a,&b);
  c=a+b;
  printf("%d+%d=%d",a,b,c);
  }
  如上程序,如果正确输入a,b的值,那么没什么问题,但是,你不能保证使用者每一次都能正确输入,一旦输入了错误的类型,你的程序不是死锁,就是得到一个错误的结果,呵呵,这可能所有人都遇到过的问题吧?解决方法:scanf()函数执行成功时的返回值是成功读取的变量数,也就是说,你这个scanf()函数有几个变量,如果scanf()函数全部正常读取,它就返回几。但这里还要注意另一个问题,如果输入了非法数据,键盘缓冲区就可能还个有残余信息问题。正确的例程:
  #include <stdio.h>
  int main()
  {
  int a,b,c; /*计算a+b*/
  while(scanf("%d,%d",&a,&b)!=2)fflush(stdin);
  c=a+b;
  printf("%d+%d=%d",a,b,c);
  }
  补充: fflush(stdin)这个方法在GCC下不可用。(在VC6.0下可以)
  以下是 C99 对 fflush 函数的定义:
  int fflush(FILE *stream);
  如果stream指向输出流或者更新流(update stream),并且这个更新流
  最近执行的操作不是输入,那么fflush函数将把任何未被写入的数据写入stream
  指向的文件(如标准输出文件stdout)。否则,fflush函数的行为是不确定的。
  fflush(NULL)清空所有输出流和上面提到的更新流。如果发生写错误,fflush
  函数会给那些流打上错误标记,并且返回EOF,否则返回0。
  由此可知,如果 stream 指向输入流(如 stdin),那么 fflush 函数的行为是不确定的。故而使用
  fflush(stdin) 是不正确的,至少是移植性不好的。
  可采用如下方法:
  /* 此函数可以和scanf函数一起使用,但使用%c输入时要注意,即此函数只能用于缓冲区非空的情况 */
  void flush()
  {
  char c;
  while ((c=getchar()) != '/n'&&c!=EOF) ;
  }
  #include <stdio.h>
  int main()
  {
  int a,b,c; /*计算a+b*/
  while(scanf("%d,%d",&a,&b)!=2) flush();
  c=a+b;
  printf("%d+%d=%d",a,b,c);
  }



  1. scanf 原型:

  2. # include <stdio.h>;
  3. int scanf( const char *format, ... );

  4. 函数 scanf() 是从标准输入流 stdin 中读内容的通用子程序,可以读入全部固有类型的数据并自动转换成机内形式。scanf() 是 printf() 的补函数。

  5. 在 C99 中,format 用 restrict 修饰。

  6. format 指向的控制串由以下三类字符组成:
  7. ● 格式说明符
  8. ● 空白符
  9. ● 非空白符
  10. 输入格式说明符前缀为百分号(%),告诉 scanf() 下次读入何种数据类型。这些格式说明符的清单如下表所示:
  11. ┏━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  12. ┃ 代码 │ 意 义 ┃
  13. ┠────┼────────────────────────────┨
  14. ┃ %a │读浮点值(仅适用于 C99) ┃
  15. ┃ %A │读浮点值(仅适用于 C99) ┃
  16. ┃ %c │读单字符 ┃
  17. ┃ %d │读十进制整数 ┃
  18. ┃ %i │读十进制、八进制、十六进制整数 ┃
  19. ┃ %e │读浮点数 ┃
  20. ┃ %E │读浮点数 ┃
  21. ┃ %f │读浮点数 ┃
  22. ┃ %F │读浮点数(仅适用于 C99) ┃
  23. ┃ %g │读浮点数 ┃
  24. ┃ %G │读浮点数 ┃
  25. ┃ %o │读八进制数 ┃
  26. ┃ %s │读字符串 ┃
  27. ┃ %x │读十六进制数 ┃
  28. ┃ %X │读十六进制数 ┃
  29. ┃ %p │读指针值 ┃
  30. ┃ %n │至此已读入值的等价字符数 ┃
  31. ┃ %u │读无符号十进制整数 ┃
  32. ┃%[ ]│扫描字符集合 ┃
  33. ┃ %% │读 % 符号(百分号) ┃
  34. ┗━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  35. 例如: %s 表示读串而 %d 表示读整数。格式串的处理顺序为从左到右,格式说明符逐一与变元表中的变元匹配。为了读取长整数,可以将 l(ell) 放在格式说明符的前面;为了读取短整数,可以将 h 放在格式说明符的前面。这些修饰符可以与 d、i、o、u 和 x 格式代码一起使用。

  36. 默认情况下,a、f、e 和 g 告诉 scanf() 为 float 分配数据。 如果将 l(ell) 放在这些修饰符的前面,则 scanf() 为 double 分配数据。使用 L 就是告诉 scanf(),接收数据的变量是 long double 型变量。

  37. 如果使用的现代编译器程序支持 1995 年增加的宽字符特性, 则可以与 c 格式代码一起,用 l 修饰符说明类型 wchar_t 的宽字符指针;也可以与 s 格式代码一起,用 l 修饰符说明宽字符串的指针。l 修饰符也可以用于修饰扫描集,以说明宽字符。

  38. 控制串中的空白符使 scanf() 在输入流中跳过一个或多个空白行。空白符可以是空格(space)、制表符(tab)和新行符(newline)。 本质上,控制串中的空白符使 scanf() 在输入流中读,但不保存结果,直到发现非空白字符为止。

  39. 非空白符使 scanf() 在流中读一个匹配的字符并忽略之。例如,"%d,%d" 使 scanf() 先读入一个整数,读入中放弃逗号,然后读另一个整数。如未发现匹配,scanf() 返回。

  40. scanf() 中用于保存读入值的变元必须都是变量指针,即相应变量的地址。

  41. 在输入流中,数据项必须由空格、制表符和新行符分割。逗号和分号等不是分隔符,比如以下代码:
  42. scanf( "%d %d", &r, &c );
  43. 将接受输入 10 20,但遇到 10,20 则失败。

  44. 百分号(%)与格式符之间的星号(*)表示读指定类型的数据但不保存。因此,
  45. scanf( "%d %*c %d", &x, &y );
  46. 对 10/20 的读入操作中,10 放入变量 x,20 放入 y。

  47. 格式命令可以说明最大域宽。 在百分号(%)与格式码之间的整数用于限制从对应域读入的最大字符数。例如,希望向 address 读入不多于 20 个字符时,可以书写成如下形式:
  48. scanf( "%20s", address );

  49. 如果输入流的内容多于 20 个字符,则下次 scanf() 从此次停止处开始读入。 若达到最大域宽前已遇到空白符,则对该域的读立即停止;此时,scanf() 跳到下一个域。

  50. 虽然空格、制表符和新行符都用做域分割符号,但读单字符操作中却按一般字符处理。例如,对输入流 "x y" 调用:
  51. scanf( "%c%c%c", &a, &b, &c );
  52. 返回后,x 在变量 a 中,空格在变量 b 中,y 在变量 c 中。

  53. 注意,控制串中的其它字符,包括空格、制表符和新行符,都用于从输入流中匹配并放弃字符,被匹配的字符都放弃。例如,给定输入流 "10t20",调用:
  54. scanf( "%dt%d", &x, &y );
  55. 将把 10 和 20 分别放到 x 和 y 中,t 被放弃,因为 t 在控制串中。

  56. ANSI C 标准向 scanf() 增加了一种新特性,称为扫描集(scanset)。 扫描集定义一个字符集合,可由 scanf() 读入其中允许的字符并赋给对应字符数组。 扫描集合由一对方括号中的一串字符定义,左方括号前必须缀以百分号。 例如,以下的扫描集使 scanf() 读入字符 A、B 和 C:
  57. %[ABC]

  58. 使用扫描集时,scanf() 连续吃进集合中的字符并放入对应的字符数组,直到发现不在集合中的字符为止(即扫描集仅读匹配的字符)。返回时,数组中放置以 null 结尾、由读入字符组成的字符串。

  59. 用字符 ^ 可以说明补集。把 ^ 字符放为扫描集的第一字符时,构成其它字符组成的命令的补集合,指示 scanf() 只接受未说明的其它字符。
  60. 对于许多实现来说,用连字符可以说明一个范围。 例如,以下扫描集使 scanf() 接受字母 A 到 Z:
  61. %[A-Z]
  62. 重要的是要注意扫描集是区分大小写的。因此,希望扫描大、小写字符时,应该分别说明大、小写字母。
  63. scanf() 返回等于成功赋值的域数的值,但由于星号修饰符而读入未赋值的域不计算在内。给第一个域赋值前已出错时,返回 EOF。

  64. C99 为 scanf() 增加了几个格式修饰符:hh、ll、j、z 和 t。hh 修饰符可用于 d、i、o、u、x、X 或 n。它说明相应的变元是 signed 或 unsigned char 值,或用于 n 时, 相应的变元是指向 long char 型变量的指针。ll 修饰符也可用于 d、i、o、u、x、X 或 n。它说明相应的变元是 signed 或者 unsigned long long int 值。
  65. j 格式修饰符应用于 d、i、o、u、x、X 或 n,说明匹配的变元是类型 intmax_t 或 uintmax_t。这些类型在 <stdint.h>; 中声明,并说明最大宽度的整数。
  66. z 格式修饰符应用于 d、i、o、u、x、X 或 n,说明匹配的变元是指向 size_t 类型对象的指针。该类型在 <stddef.h>; 中声明,并说明 sizeof 的结构。
  67. t 格式修饰符应用于 d、i、o、u、x、X 或 n,说明匹配的变元是指向 ptrdiff_t类型对象的指针。该类型在 <stddef.h>; 中声明,并说明两个指针之间的差别。


  68. 例子:

  69. # include <stdio.h>;

  70. int main( void )
  71. {
  72. char str[80], str2[80];
  73. int i;

  74. /* read a string and a integer */
  75. scanf( "%s%d", str, &i );

  76. /* read up to 79 chars into str */
  77. scanf( "%79s", str );

  78. /* skip the integer between the two strings */
  79. scanf( "%s%*d%s", str, str2 );

  80. return 0;
  81. }


  82. 相关函数:
  83. printf() 及 fscanf()。
复制代码


Consider the following code:

#include <stdio.h>

int main (void)
{
 char str1[128], str2[128], str3[128];

 printf ("\nEnter str1: ");
 scanf ("%[^\n]", str1);
 printf ("\nstr1 = %s", str1);

 printf ("\nEnter str2: ");
 scanf ("%[^\n]", str2);
 printf ("\nstr2 = %s", str2);

 printf ("\nEnter str3: ");
 scanf ("%[^\n]", str3);
 printf ("\nstr2 = %s", str3);

 printf ("\n");
 return 0;
}

When it is executed only the first scanf stops for the prompt. The program does not stop for the nextscanf s. But if the format string is changed from "%[^\n]" to" %[^\n]" (note the blank space before %), then it works okay. Does some existing newline character from the previous input buffer is automatically accepted ? But flushingstdin does not solve this.

What is the cause of this.



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics