输入多个数字时,数字间用空格隔开和数字间间用回车隔开有什么区别

HiDraling 2011-11-16 03:30:50
比如说我用scanf("%d%d%d%d",&a,&b,&c,&d)输入四个数字,当我键入数字5后,我按回车,编译器怎么不结束输入,不是说回车是结束输入吗
...全文
965 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
HiDraling 2011-11-16
  • 打赏
  • 举报
回复
谁能用中文解释下吗,英文看得发晕,都不知道自己看懂了没有
赵4老师 2011-11-16
  • 打赏
  • 举报
回复
没有区别。
请检查scanf的返回值。
以下内容摘自MSDN98:
Format Specification Fields: scanf and wscanf Functions
A format specification has the following form:

%
  • [width] [{h | l | 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 stdin 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 stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin 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.
HiDraling 2011-11-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ys793914049 的回复:]

还有你标题和内容不协同啊 你要问回车是怎么隔开字符还是怎么结束 回车就是另起一行 你小学写作文都写成一堆么?? 回车结束貌似应该要先定义的把
[/Quote]的确回车是起到了换行作用,我想问的是还是那条代码,当运行到那时它会要我输入数字吧,我输入第一个数字5,当我输入第二个数字前我不用空格隔开,而是用回车,这时命令行窗口中光标换了行,但没有结束输入,我想问的是它为什么不结束,而是继续要你输入,直到四个数字输完,再按回车才有结束输入的作用
csuyuanweiqingking 2011-11-16
  • 打赏
  • 举报
回复
scanf(),前面的%d,%c之类决定了你后面的输入变量。你的前面是%d,所以对于你输入回车之类的没有影响。
AnYidan 2011-11-16
  • 打赏
  • 举报
回复
scanf() reads from stream under control of format, and assigns converted values through subsequent arguments, each of which must be a pointer. It returns when format is exhausted.

A conversion specification determines the conversion of the next input field. .... An input field is defined as a string of non-white space characters; it extends either to the next white space character or until the field width, if specified,
AnYidan 2011-11-16
  • 打赏
  • 举报
回复
scanf("%d %d %d %d",&a,&b,&c,&d); 输入 1 2 3 4 enter

自信男孩 2011-11-16
  • 打赏
  • 举报
回复
scanf("%d%d%d%d",&a,&b,&c,&d);scanf()并不是以你说的回车结束输入,输入要根据你的格式化符"%d"来确定的。对于"%d",你可以每次输入后按空格,也可以每次输入后按回车。都可以正确输入,但是你输入后按空格,即使你多输入了一个也不会影响结果。但是如果是其他格式化符就不一样了,比如%c,这个格式化符就比较难处理,需要另外加一些处理,比如getchar();或fflush(stdin);等等。
你可以找一些专门讲解scanf()函数的资料学一学。
HiDraling 2011-11-16
  • 打赏
  • 举报
回复
谢谢大家,似乎明白了,我再好好想想
ys793914049 2011-11-16
  • 打赏
  • 举报
回复
还有你标题和内容不协同啊 你要问回车是怎么隔开字符还是怎么结束 回车就是另起一行 你小学写作文都写成一堆么?? 回车结束貌似应该要先定义的把
ys793914049 2011-11-16
  • 打赏
  • 举报
回复
其他的不清楚了 我学C++ c懂一些 我准备等以后在追学C的 扩展知识面的
ys793914049 2011-11-16
  • 打赏
  • 举报
回复
你代码不全啊 你没给它们一个输出口啊 也没定义回车结束程序
goldbeef 2011-11-16
  • 打赏
  • 举报
回复
scanf() reads from stream under control of format, and assigns converted values through subsequent arguments, each of which must be a pointer. It returns when format is exhausted.

A conversion specification determines the conversion of the next input field. .... An input field is defined as a string of non-white space characters; it extends either to the next white space character or until the field width, if specified,

翻译如下:
scanf函数在格式说明符的控制下从流中读取数据,然后将数据转换成 后续的指针参数 对应的数据类型。
只有当每个控制说明符(一般以%开始)对应的数据都读入,scanf函数才返回

每一次转换 取决于 下一个输入域。。。输入域定义为 没有空格的字符串,逐个字符读入直到遇到下一个空白字符串,或者 所确定的域宽度。

//翻译的好费劲

69,382

社区成员

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

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