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. Bothi
andj
will hold the given values,100
and200
, respectively.For an input like
100 - 200
, the scanning will fail (matching failure) and the content ofj
will remain unchanged, i.e.,j
is 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.