First of all, your code has the following serious problem:
You are not allocating sufficient space for scanf
to write a character. Passing an insufficiently large memory buffer to scanf
causes undefined behavior.
You should change char a[0];
to char a[1];
, or better: Simply write char a;
and change scanf("%c", a);
to scanf("%c", &a);
.
Now, to answer your actual question:
You are not taking into account that the newline character '\n'
is also part of the input. It is this character that you are registering as an additional character.
If you want the newline character to be discarded, then you could change
scanf( "%c", &a );
to:
scanf( " %c", &a );
This will cause scanf
to discard all whitespace characters (i.e. spaces, tabs, newlines, etc.) before attempting to match a character. That way, scanf
will never match a newline character (or any other whitespace character).
If you only want newline characters to be discarded, but not spaces and other whitespace characters, then you can use these lines instead:
//discard any newline characters
scanf( "%*[\n]" );
//read the actual input
scanf( "%c", &a );
Note that in the code above, it is necessary to have two separate scanf
function calls. You cannot combine them like this:
//read the actual input
scanf( "%*[\n]%c", &a );
The reason why you cannot do this is that the %[]
conversion format specifier will only match a non-empty character sequence. Therefore, matching will fail if there are no newline characters. In the line scanf( "%*[\n]" );
, it does not matter whether matching succeeds or fails, but in the line scanf( "%*[\n]%c", &a );
, it does matter, because scanf
won't attempt to match the second conversion format specifier if matching the first one fails.
See the documentation for the function scanf
for further information.