.TH gets() "" "" "STDIO Function (libc)"
.PC "Read string from standard input"
.B "#include <stdio.h>"
\fBchar *gets(\fIbuffer\^\fB)\fR
\fBchar *\fIbuffer\^\fB;\fR
.PP
.B gets()
reads characters from the standard input into a buffer pointed at by
\fIbuffer\fR.
It stops reading as soon as it detects a newline character or EOF.
.B gets()
discards the newline or EOF, appends NUL onto the string it has built,
and returns another copy of
.IR buffer .
.SH Example
The following example uses \fBgets()\fR to get a string from the
console; the string is echoed twice to demonstrate what \fBgets()\fR
returns.
.DM
#include <stdio.h>
.sp \n(pDu
main()
{
	char buffer[80];
.DE
.DM
	printf("Type something: ");
.DE
.DM
	fflush(stdout);
	printf("%s\en%s\en", gets(buffer), buffer);
}
.DE
.SH "See Also"
.Xr "buffer," buffer
.Xr "fgets()," fgets
.Xr "getc()," getc
.Xr "libc" libc
.br
\*(AS, \(sc7.9.7.7
.br
\*(PX Standard, \(sc8.1
.SH Diagnostics
.B gets()
returns NULL if an error occurs or if
EOF is seen before any characters are read.
.SH Notes
.B gets()
stops reading the input string as soon as it detects a newline
character.
If a previous input routine left a newline character in the
standard input buffer,
.B gets()
will read it and immediately stop accepting characters;
to the user, it will appear as if
.B gets()
is not working at all.
.PP
For example, if
.B getchar()
is followed by
.BR gets() ,
the first character \fBgets()\fR will receive is the newline
character left behind by \fBgetchar()\fR.
A simple statement will remedy this:
.DM
	while (getchar() != '\en')
		;
.DE
.PP
This throws away the newline character left behind by
.BR getchar() ;
.B gets()
will now work correctly.
