What happens if scanf () gets some character that doesn't match the format string?

#include <stdio.h>

int main(void)
{
    int i, j, k;
    scanf("%d%d%d", &i, &j, &k);
    printf("%d %d %d", i, j, k);
    return 0;
}

If you enter 1,2,3, what will happen? And why?

According to https://stackoverflow.com/a/1681826/ ... if it scanf()reads an unexpected row, it will return early without changing the value after the last successful value.

I tried clang (LLVM 6.1.0), -O0the above explanation is correct, but the -O2second variable is always a random number, but not the same as before scanf(), and the Third variable is always 0.

+4
source share
2 answers

, scanf , .

; . , EOF. , , EOF

1,2,3, scanf 1, , ,, .

j k, , , undefined, , , . , scanf, , , .

+4

, . scanf , , . j k . , undefined, .

0

All Articles