COHERENT manpages

This page displays the COHERENT manpage for gets() [Read string from standard input].

List of available manpages
Index


gets() -- STDIO Function (libc)

Read string from standard input
#include <stdio.h>
char *gets(buffer)
char *buffer;

gets() reads characters from the standard input into a buffer pointed at by
buffer.  It stops reading as soon as it detects a newline character or EOF.
gets() discards  the newline  or EOF,  appends NUL onto  the string  it has
built, and returns another copy of buffer.

Example

The following  example uses gets()  to get a  string from the  console; the
string is echoed twice to demonstrate what gets() returns.

#include <stdio.h>

main()
{
    char buffer[80];

    printf("Type something: ");

    fflush(stdout);
    printf("%s\n%s\n", gets(buffer), buffer);
}

See Also

buffer,
fgets(),
getc(),
libc
ANSI Standard, §7.9.7.7
POSIX Standard, §8.1

Diagnostics

gets()  returns NULL  if  an error  occurs  or if  EOF is  seen before  any
characters are read.

Notes

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, gets() will read it  and immediately stop accepting
characters; to the user, it will appear as if gets() is not working at all.

For example, if getchar() is followed by gets(), the first character gets()
will receive  is the newline character left behind  by getchar().  A simple
statement will remedy this:

    while (getchar() != '\n')
        ;

This throws  away the  newline character  left behind by  getchar(); gets()
will now work correctly.