Search code examples
cscanf

Multiple testing scanf result - C


I am new in c programming and i need to scan variable if variable is EOF or non-number.

My code is this:

 while (scanf("%d", &var) != EOF) {
code...
  }

I need something like this:

while ((scanf("%d", &var) != EOF)||(scanf("%d", &var) != 1)) {
    code...
      }

Problem is that when i use scanf once it will scan input and second scanf waits for next input and wont test the actual one.

Thank for any help.


Solution

  • Put the result of scanf in a variable so you can test it again.

    while ((res = scanf("%d", &var)) != EOF && res != 1) {
        code ...
    }