简体   繁体   English

为什么第二个scanf()不会执行

[英]Why won't the second scanf() execute

I'm trying to execute this block of code. 我正在尝试执行这段代码。

#include <stdio.h> 
int main(void)
{       

     printf("Start from here\n");
     int e, f, g, h;

     scanf("%d,%d", &e, &f);
     scanf("%d, %d", &g, &h);

     printf("%d %d %d %d", e, f, g, h);
}

When I input 2,0 or something that matches the format string in the first scanf() , the second scanf() also executes. 当我输入2,0或与第一个scanf()中的格式字符串匹配的东西时,第二个scanf()也会执行。

However, if I input something like 2-0 in the first scanf() , the program skips the second scanf() and goes straight to the printf() 但是,如果我在第一个scanf()输入类似2-0的内容,程序将跳过第二个scanf()并直接进入printf()

For example, here's the input and output of a sample run of the program. 例如,这是程序样本运行的输入和输出。 The second line is the input. 第二行是输入。

Start from here
1-2  
1 0 -2 -856016624u

Notice how the program completely skipped the second scanf() , and went straight to the printf() . 注意程序如何完全跳过第二个scanf() ,然后直接进入printf() Why is the second scanf() being skipped over here? 为什么在这里跳过第二个scanf()

scanf 's format string cares about the non-format specifiers in it too. scanf的格式字符串也关注其中的非格式说明符。 When you write "1-2" the first scanf will read "1", then look for a comma. 当您写“1-2”时,第一个scanf将显示为“1”,然后查找逗号。 It won't find one, so it'll give up. 它不会找到一个,所以它会放弃。 Now, the second scanf will see "-2" and then look for a comma. 现在,第二个scanf将看到“-2”,然后查找逗号。 It won't find one, so it'll give up. 它不会找到一个,所以它会放弃。

The end result is that the other two variables won't get set so they end up being whatever garbage is in their memory location at execution time. 最终结果是其他两个变量不会被设置,因此它们最终会成为执行时内存位置中的垃圾。

You can avoid this by checking the return value of scanf. 您可以通过检查scanf的返回值来避免这种情况。 It'll tell you how many values it found. 它会告诉你它找到了多少值。 Try this: 尝试这个:

#include <stdio.h> 
int main(void)
{       

     printf("Start from here\n");
     int e, f, g, h;

     if (scanf("%d,%d", &e, &f) != 2) { /* error handling */ }
     if (scanf("%d, %d", &g, &h) != 2) { /* error handling */ }

     printf("%d %d %d %d", e, f, g, h);
}

Remove a comma between the two format specifier. 删除两个格式说明符之间的逗号

scanf("%d %d", &e, &f); // Remove comma in first argument of scanf
scanf("%d %d", &g, &h);  // Remove comma in first argument of scanf
        ^^^
      Remove comma between two numbers

Because scanf will only skip white space and the comma is not white space . 因为scanf只会跳过空格comma不是空格

What actually happens when you ask scanf to read numeric data is that it first skips any white space it finds and then it reads characters until the character it reads cannot form part of a number. 当你要求scanf读取数字数据时实际发生的事情是它首先跳过它找到的任何空格,然后它读取字符,直到它读取的字符不能形成数字的一部分。

In this case, when it encounters the comma , it stops reading. 在这种情况下,当遇到逗号时 ,它会停止读取。 Since it has not read any digits, there is no number for it to store, so it simply leaves the original value. 由于它没有读取任何数字,因此没有数字可供存储,因此它只保留原始值。

In your code, scanf("%d, %d",&e,&f) is in this way, so you should give your input like: 1,2 or 2,3 etc 在你的代码中, scanf("%d, %d",&e,&f)是这样的,所以你应该给你的输入如: 1,22,3

If you want your input to be given like 0-2 or 2-4 , then your scanf must be like this : scanf("%d-%d",&e,&f) . 如果您希望输入为0-22-4 ,则scanf必须如下: scanf("%d-%d",&e,&f)

In this way, it will not be skipped. 这样就不会跳过它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM