Search code examples
cscanfstdineof

I input Ctrl - D (EOF) when called scanf(), then next scanf ignored


I input Ctrl - D (EOF) when called scanf(), then next scanf ignored.

#include <stdio.h>

int main()
{
   int input;
   scanf("%d", &input);//I press Ctrl-D when this line
   scanf("%d", &input);//this line just passed. not read my input. why?
   return 0;

}

I want get input with scanf() second called. What's is a problem?


Solution

  • it seems that your code is OK. The only think is to remember to press "return" at the end of both input number. I rewrite your code in a "better" way:

    #include <stdio.h>
    int main(){
    
    int input1,input2;
    printf("Digit the first number: ");
    scanf("%i", &input1);//Press return at the end
    printf("Digit the second number: ");
    scanf("%i", &input2);//Press return at the end
    printf("input1:%i\tinput2:%i\n",input1,input2);
    return 0;
    }