输入期间两个整数之间的逗号

Comma between the two integers during the input

如果我执行以下操作会发生什么

scanf("%d,%d", &i, &j);

并提供导致匹配失败的输入?它会把垃圾存入j吗?

输入必须完全匹配所提供的格式才能使scanf()成功。

引用 C11,章节 §7.21.6.2,fsacnf(),(强调我的

Except in the case of a % specifier, the input item (or, in the case of a %n directive, the count of input characters) is converted to a type appropriate to the conversion specifier. If the input item is not a matching sequence, the execution of the directive fails: this condition is a matching failure. Unless assignment suppression was indicated by a *, the result of the conversion is placed in the object pointed to by the first argument following the format argument that has not already received a conversion result. If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.

并且,

When all directives have been executed, or if a directive fails (as detailed below), the function returns.

所以,综合以上案例,

  • 100, 200这样的输入,扫描就会成功。 ij 将分别保存给定值 100200

  • 对于100 - 200这样的输入,会扫描失败(匹配失败),j的内容保持不变,即j不是通过scanf()操作分配任何值。

忠告:始终检查 scanf() 函数族的 return 值以确保函数调用成功。