C/C++ Arena

Step 5 of 5

Did the read work?

scanf returns how many values it successfully read. If the input isn't a number, reading %d fails and returns 0. At the end of input it returns EOF (a negative number).

int n;
if (scanf("%d", &n) == 1) {
    // n is valid
}

You'll learn if properly in the next module; for now just follow the pattern.

Your turn: read one int. If it worked, print Got N. Otherwise print Not a number.

Previous: Characters and words