What exactly happens if I do the following
scanf("%d,%d", &i, &j);
and provide an input which causes the matching failure? Will it store garbage into j?
What exactly happens if I do the following
scanf("%d,%d", &i, &j);
and provide an input which causes the matching failure? Will it store garbage into j?
The input has to exactly match the supplied format for
scanf()to be success.Quoting
C11, chapter ยง7.21.6.2,fsacnf(), (emphasis mine)and,
So, consolidating the above cases,
For an input like
100, 200, the scanning will be success. Bothiandjwill hold the given values,100and200, respectively.For an input like
100 - 200, the scanning will fail (matching failure) and the content ofjwill remain unchanged, i.e.,jis not assigned any value byscanf()operation.Word of advice: always check the return value of
scanf()function family to ensure the success of the function call.