从scanf谈起:数组
int scanf( const char *format [, argument]... ); int _scanf_l( const char *format, locale_t locale [, argument]... ); int wscanf( const wchar_t *format [, argument]... ); int _wscanf_l( const wchar_t *format, locale_t locale [, argument]... );
Return Value
Returns the number of fields successfully 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.
If format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions return EOF and set errno to EINVAL.
For information on these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.ide
// crt_scanf.c // compile with: /W3 /* This program uses the scanf and wscanf functions * to read formatted input. */ #include <stdio.h> int main( void ) { int i, result; float fp; char c, s[81]; wchar_t wc, ws[81]; result = scanf( "%d %f %c %C %80s %80S", &i, &fp, &c, &wc, s, ws ); // C4996 // Note: scanf and wscanf are deprecated; consider using scanf_s and wscanf_s printf( "The number of fields input is %d\n", result ); printf( "The contents are: %d %f %c %C %s %S\n", i, fp, c, wc, s, ws); result = wscanf( L"%d %f %hc %lc %80S %80ls", &i, &fp, &c, &wc, s, ws ); // C4996 wprintf( L"The number of fields input is %d\n", result ); wprintf( L"The contents are: %d %f %C %c %hs %s\n", i, fp, c, wc, s, ws); } 71 98.6 h z Byte characters 36 92.3 y n Wide characters The number of fields input is 6 The contents are: 71 98.599998 h z Byte characters The number of fields input is 6 The contents are: 36 92.300003 y n Wide characters
天然数:符合输入格式的数据的个数函数
0:不符合输入格式的数据(若是Ctrl+Z和回车键之间有其余字符,则也返回0)操作系统
以"%d"为例:要求是十进制整数,若是输入字符'r'、'e'、'\'等,就返回0,天然地,'\n''\0''eof'也返回0,这与他们自己的含义(好比换行符、终止符、文件结束符)并没有关系
-1:Ctrl+Z(紧接着按下回车键)翻译
注:Windows系统中通常采用阻塞式检查 Ctrl+Z、Unix/Linux系统下通常采用非阻塞式的检查 Ctrl+D。本程序是在Windows系统下,所以使用阻塞式的 Ctrl+Z 来标识流的结束。code
阻塞式方式的特色:orm
只有按下回车以后才有可能检测在此以前是否有Ctrl+Z按下。文档
(按照输入时间顺序读取输入缓冲区的数据)读取到Ctrl+Z时,若是后面有可读的数据,则不会理睬Ctrl+Z,也就是不认为Ctrl+Z表明着流的末尾。(由于有要读的数据,还不能认为到了流的末尾)。字符串
Ctrl+Z产生的不是一个普通的ASCII码值,也就是说它产生的不是一个字符,因此不会跟其它从键盘上输入的字符同样可以存放在输入缓冲区。get
将键盘上敲下的字符送入输入缓冲区。
若是用户在按回车键以前输入了不仅一个字符,其余字符会保留在键盘缓冲区中,等待后续的输入函数(好比scanf()、getchar())调用读取。也就是说,后续的scanf()调用不会等待用户按键,而是直接读取缓冲区中的字符,直到缓冲区的字符读取完毕后,才等待用户按键。
对于scanf(),只有字符char在输入流中的获取会认可空格或回车中的换行符为所要取的值,别的如字符串或者字符数组或int类型均不认为空格或回车中的换行符为其值即丢弃空格符和回车符,以空格做为划分。
'\n'、'\0'、'eof'是C/C++编译器在编译代码时识别的符号。
Ctrl+Z是操做系统在处理输入流时识别的符号。
'\n'换行符,
#include <stdio.h> int main() { int c; do { printf("请输入文档的结尾标志"); }while((c=getchar())!='\n'); printf("已获得文档结束标志\n"); //直接回车 return 0; }
'\0'终止符
'\0' is the null termination character. It marks the end of the string.
char cAlphabet[] = "I know all about programming!";
is the same as
char cAlphabet[] = {'I',' ', 'k','n','o','w',' ','a','l','l',' ','a','b','o','u','t',' ','p','r','o','g','r','a','m','i','n','g','!','\0'};
#include <stdio.h> int main() { int c; do { printf("请输入文档的结尾标志"); }while((c=getchar())!='\0'); printf("已获得文档结束标志\n"); return 0; }
'eof'文件结束符
#include <stdio.h> int main() { int c; do { printf("请输入文档的结尾标志"); }while((c=getchar())!=EOF); printf("已获得文档结束标志\n"); //Ctrl+Z而后回车 return 0; }
在控制台输入的时候,操做系统将Ctrl+Z翻译为文档结束符。