user-0 提问: 使用Dev C++5.1.1 scanf输入多个值的问题

回复:
user: user-1
content: 确实如楼主所说，即使输入时按逗号要求输入，也是得不到正确的值 不知道别的编译器是不是 但这样题目能过，我猜是编译器的原因

user: user-2
content: 引用 11 楼 zhao4zhong1 的回复:[quote=引用 9 楼 zhao4zhong1 的回复:] 6楼的代码实现了输入两个整数时随便你怎么分隔。 再＋10086[/quote] 对输入容错才是好的设计。

user: user-3
content: scanf("%d %d %d",&a,&b,&c)，由于 整型和浮点型数据读取的时候， 缺省会跳过（吃掉）所有前导空白（空格符，制表符，回车符等空白符） 所以加个空格，跟没加空格，效果是一致的 ------------------------------------------------------------------------  格式串中的空格作用是跳白，也就是吃掉空白符 ---跳过空白符，直到遇到非空白符号为止。------------- 所以： 不打算输入空行，以及前导空白，可以在%s格式前加空格 不打算输入空白字符，也可以在%c的前面加上空格

user: user-4
content: 指明了什么样的格式，就要有什么样的输入。  "%d" "%d" "%d"其实就是"%d%d%d" 对应输入：1<space>2<space>3  "%d %d %d"由于格式中有额外的空格，所以输入中除了原先的空格之外，还需要有对应的额外的空格： 对应输入：1<space><space>2<space><space>3  "%d,%d,%d"由于格式中用于分隔数字的符号是,，所以输入中要用,来分隔 对应输入：1,2,3  指明了什么样的格式，就要有什么样的输入。

user: user-3
content: scanf 格式串中非格式字符部分，表示期待输入的（数据以外的）字符  格式串也是字符串，所以字符串连接，字符串中的转义序列，对格式串同样适用。 只有% 号序列，是格式串独有的序列，表示要输入的数据类型，格式。

user: user-3
content: "%d" "%d" "%d"这是三个字符串连接成一个字符串 中间的空格被忽略

user: user-3
content: 第一个 自动跳白 ，格式等同于 “%d%d%d”，输入的时候，依然需要空格，等空白符号分开数据。不然就会产生输入混乱 第二个按照格式跳白-----空格的作用是跳白----- 第三个输入逗号作为分隔符，并跳过逗号，自动跳白 这几个格式本身并没有错误 只是 格式串，和大家所能想象的输入格式未必相同，和预期的格式不太一致。

user: user-2
content: 引用 9 楼 zhao4zhong1 的回复:6楼的代码实现了输入两个整数时随便你怎么分隔。 再＋10086

user: user-5
content: 引用 2 楼 paschen 的回复:第一种是错的 第二种输入时可用空格分 第三种输入时要用逗号分隔 在+1

user: user-2
content: 6楼的代码实现了输入两个整数时随便你怎么分隔。

user: user-6
content: 这个也不好解释吧..... http://download.csdn.net/detail/huangzhengdoc/9618890 还是自己看一下吧......

user: user-7
content: 引用 2 楼 paschen 的回复:第一种是错的 第二种输入时可用空格分 第三种输入时要用逗号分隔  +1

user: user-8
content: http://en.cppreference.com/w/c/io/fscanf

user: user-2
content: 仅供参考：#include <stdio.h> int main() {     int a,b,n,v,r,k;     char buf[80];     char *p;      k=0;     r=EOF;     while (1) {         if (EOF==r) {             fgets(buf,80,stdin);             p=buf;         }         while (1) {             r=sscanf(p,"%d%n",&v,&n);             if (1==r) {k++;break;}             if (EOF==r) break;             p++;         }         if (EOF==r) continue;         p+=n;         if (1==k) a=v;         if (2==k) {             b=v;             break;         }     }     printf("%d,%d\n",a,b);     return 0; }

user: user-9
content: 引用 2 楼 paschen 的回复:第一种是错的 第二种输入时可用空格分 第三种输入时要用逗号分隔 c语言连着写的字符串常量编译器会自动合并的。

user: user-2
content: 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.  不要迷信书、考题、老师、回帖； 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实！

user: user-10
content: 验证了确实如此。 VS就不是这样？

user: user-8
content: 第一种是错的 第二种输入时可用空格分 第三种输入时要用逗号分隔

user: user-11
content: 你写成这种格式scanf("%d,%d,%d",&a,&b,&c)，意味着你输入的时候也要用逗号分隔，而不是空格 比如:1,2,3