There are several errors in your code.
First, and this is very important: Do not use gets()
ever again. This is a
dangerous function: Why is the gets function so dangerous that it should not be used?
Besides gets
is not included in C11 any more.
Second, you need to understand what happens when you input data with the
keyboard. When you enter a text you have to press ENTER for the
reading routing to continue. The ENTER also gets stored in the input
buffer as the newline ('\n'
) character. So if you enter abc the
buffer will contain1:
+---+---+---+----+
| a | b | c | \n |
+---+---+---+----+
This is important to remember, because for example fgets
stop reading from
the buffer when it encounters the newline character2. Other functions
like scanf
may read the newline depending on the conversion specifier used.
You use getchar
.
man getchar
#include <stdio.h>
int fgetc(FILE *stream);
int getc(FILE *stream);
int getchar(void);
DESCRIPTION
fgetc()
reads the next character from stream and returns it as an unsigned char
cast to an int
, or EOF
on end of file or error.
getc()
is equivalent to fgetc()
except that it may be implemented as a macro which evaluates stream more than once.
getchar()
is equivalent to getc(stdin)
.
it is clear from the documentation that getchar()
reads one character
only. At this point it's important to notice that all reading functions like
fgets
, scanf
, etc. will first try to read from the input buffer and only
when the input buffer is empty and they still need to keep reading, then they
will wait for user input. That means if a previous read function left characters
in the input buffer, the next read function might not wait for user input,
because it might read from the input buffer.
So, let's say you entered aENTER. The input buffer will
look like this:
+---+----+
| a | \n |
+---+----+
The input buffer will have at least 2 characters in it, 'a'
and the newline.
getchar
reads the first and returns. The input buffer look now like this:
+----+
| \n |
+----+
Then you execute gets(b);
, it will see that there are characters left in
the input buffer, hence it will read from it. It reads the newline and gets
stops reading, because gets
stops reading, because of the newline that
was in the input buffer. gets
didn't need to wait for user input. As
an user you have the impression that the gets
call was skipped.
Finally:
char b[2];
puts(b);
This is undefined behaviour, because b
is an uninitialized array, meaning that
the values b[0]
and b[1]
are undefined (unknown, they may be 0, or -2323 or
2829281, nobody knows). puts
expects a String. In C a string is sequence
of non-zero bytes that end with the value 0 (the so called '\0'
-terminating
byte). b
is not a string, so doing puts(b)
yields undefined behaviour.
Footnotes
1I don't know if the input buffer also stores a '\0'
-terminating
byte after the newline. In any case it is not important here.
2Provided that the destination buffer is large enough. If there is no
more space in the destination buffer, fgets
stop reading anyway.