Why one more charater in stdin?

I try to use the function "fgets()" to read the input from keyboard. My code is looks like:

char A[2], B[2];

fgets(A, 2, stdin);
fgets(B, 2, stdin);

I found a weird thing if I type a charater in keyboard, then press "enter". I can see the 2nd fgets (fgets(B, 2, stdin);) is skipped. I think it has not been skipped, it is because there is a charater in stdin after I type the charater and press "enter". But I wonder since I only type one character and "enter", where the one extra character comes from?

The reason I guess the problem is an extra charater is because the problem can be fixed thru:

char A[3], B[2];

fgets(A, 3, stdin);
fgets(B, 2, stdin);

if I increase to 3 but still type one character, the 2nd command will not be skipped any more. Someone can help me this?

Thanks in advance.
The extra character is a newline character '\n'. It's inserted when you press enter.
http://en.cppreference.com/w/cpp/io/c/fgets
Parsing stops if end-of-file occurs or a newline character is found, in which case str will contain that newline character.
Thanks for reply. So do you mean in A[3], it is:
A[0] = 'Y'
A[1] = '\0'
A[2] = '\n'

?
it is "Y\n\0"
Topic archived. No new replies allowed.