scanf("%d",&n)与while(scanf("%d",&n)!=EOF)用的时候有什么不同

lalalallalalalala 2015-07-23 03:44:00
为什么输入数据N的时候,有时候用第一种,有时候用第二种,求解~~~
...全文
567 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-07-24
  • 打赏
  • 举报
回复
英语也是一门计算机语言的说。 Return Value Both scanf and wscanf return 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. 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.
lm_whales 2015-07-24
  • 打赏
  • 举报
回复
scanf只有输入 CTRL +Z (CTRL +D) 这种表示文件结束的 数据时候,才会返回 EOF 其他时候,返回成功输入的数据个数 当只有一个数据要输入的时候,成功 返回1 ,失败返回0,文件结束 返回 EOF。 这里的数据个数, 是指格式串指定的,输入数据个数, 和调用参数个数,关系不大。 while (scanf("%d", &n) != EOF) 表示程序 以 CTRL +Z (CTRL +D)结束这个循环输入。 CTRL +Z :DOS,Windows CTRL +D:Unix,linux 等系统
自信男孩 2015-07-24
  • 打赏
  • 举报
回复
引用 5 楼 fly_dragon_fly 的回复:
scanf成功返回读入参数的个数,失败返回EOF
赞同~ while (scanf("%d", &n) != EOF)只有在输入即scanf返回错误时才能退出循环。scanf的返回值是输入的数据的个数,这个scanf若返回成功则会返回1;
fly_dragon_fly 2015-07-24
  • 打赏
  • 举报
回复
scanf成功返回读入参数的个数,失败返回EOF
二班的码农 2015-07-24
  • 打赏
  • 举报
回复
一个是单个输入,一个是循环多个输入,后一个通过EOF来判断是否已全部读完
lalalallalalalala 2015-07-23
  • 打赏
  • 举报
回复
引用 2 楼 赵4老师的回复:
scanf, wscanf Read formatted data from the standard input stream. int scanf( const char *format [,argument]... ); int wscanf( const wchar_t *format [,argument]... ); Routine Required Header Compatibility scanf <stdio.h> ANSI, Win 95, Win NT wscanf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Both scanf and wscanf return 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. 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. Parameters format Format control string argument Optional arguments 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. wscanf is a wide-character version of scanf; the format argument to wscanf is a wide-character string. wscanf and scanf behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tscanf scanf scanf wscanf For more information, see Format Specification Fields — scanf functions and wscanf Functions. Example /* SCANF.C: This program uses the scanf and wscanf functions * to read formatted input. */ #include <stdio.h> void main( void ) { int i, result; float fp; char c, s[81]; wchar_t wc, ws[81]; printf( "\n\nEnter an int, a float, two chars and two strings\n"); result = scanf( "%d %f %c %C %s %S", &i, &fp, &c, &wc, s, ws ); printf( "\nThe 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); wprintf( L"\n\nEnter an int, a float, two chars and two strings\n"); result = wscanf( L"%d %f %hc %lc %S %ls", &i, &fp, &c, &wc, s, ws ); wprintf( L"\nThe 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); } Output Enter an int, a float, two chars and two strings 71 98.6 h z Byte characters The number of fields input is 6 The contents are: 71 98.599998 h z Byte characters Enter an int, a float, two chars and two strings 36 92.3 y n Wide characters The number of fields input is 6 The contents are: 456 92.300003 y n Wide characters Floating-Point Support Routines, Stream I/O Routines, Locale Routines See Also fscanf, printf, sprintf, sscanf
神(๑•ั็ω•็ั๑)看不懂……
赵4老师 2015-07-23
  • 打赏
  • 举报
回复
scanf, wscanf Read formatted data from the standard input stream. int scanf( const char *format [,argument]... ); int wscanf( const wchar_t *format [,argument]... ); Routine Required Header Compatibility scanf <stdio.h> ANSI, Win 95, Win NT wscanf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Both scanf and wscanf return 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. 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. Parameters format Format control string argument Optional arguments 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. wscanf is a wide-character version of scanf; the format argument to wscanf is a wide-character string. wscanf and scanf behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tscanf scanf scanf wscanf For more information, see Format Specification Fields — scanf functions and wscanf Functions. Example /* SCANF.C: This program uses the scanf and wscanf functions * to read formatted input. */ #include <stdio.h> void main( void ) { int i, result; float fp; char c, s[81]; wchar_t wc, ws[81]; printf( "\n\nEnter an int, a float, two chars and two strings\n"); result = scanf( "%d %f %c %C %s %S", &i, &fp, &c, &wc, s, ws ); printf( "\nThe 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); wprintf( L"\n\nEnter an int, a float, two chars and two strings\n"); result = wscanf( L"%d %f %hc %lc %S %ls", &i, &fp, &c, &wc, s, ws ); wprintf( L"\nThe 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); } Output Enter an int, a float, two chars and two strings 71 98.6 h z Byte characters The number of fields input is 6 The contents are: 71 98.599998 h z Byte characters Enter an int, a float, two chars and two strings 36 92.3 y n Wide characters The number of fields input is 6 The contents are: 456 92.300003 y n Wide characters Floating-Point Support Routines, Stream I/O Routines, Locale Routines See Also fscanf, printf, sprintf, sscanf
MARIOV 2015-07-23
  • 打赏
  • 举报
回复
一个一次,一个循环的吧

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧