Given the input 2.3
, the space in the format character causes any whitespace to be skipped (there is none), and the first %d
causes a value of 2
to be read. The next character to be read will be the .
, and that will remain in the buffer. scanf()
, when handling the second %d
will encounter that '.'
, deem that character is not part of an integral value, and terminate reading, again leaving the '.'
in the stream to be read.
The net effect of scanf(" %d%d", &a, &b)
is therefore to read 2
to a
, and there is no change of b
.
Unless your code does something to read the '.'
(e.g. read it using the %c
format), that .
will continue to be in the stream, and every subsequent usage of %d
will fail in the same way.
Your code is not checking the return value from scanf()
but, if it did, would detect another consequence of the problem. The return value of scanf()
will then be 1
, indicating that one value (a
) was successfully read. If both the integers a
and b
had been successfully read, the value 2
would be returned.
Since b
is uninitialised in main()
, accessing its value gives undefined behaviour, and scanf()
doesn't change that fact. One possible consequence of undefined behaviour is the "weird output" you are seeing.