

gets()                    STDIO Function                   gets()




Read string from standard input

#include <stdio.h>
cchhaarr *ggeettss(_b_u_f_f_e_r) cchhaarr *_b_u_f_f_e_r;

gets  reads characters  from  the standard  input  into a  buffer
pointed at by  _b_u_f_f_e_r.  It stops reading as soon  as it detects a
newline character or EOF.   gets discards the newline or EOF, ap-
pends a null character onto  the string it has built, and returns
another copy of _b_u_f_f_e_r.

***** Example *****

The following example uses ggeettss to get a string from the console;
the string is echoed twice to demonstrate what ggeettss returns.


#include <stdio.h>

main()
{
        char buffer[80];



        printf("Type something: ");



        /*
         * because of the way COHERENT's teletype
         * driver works, the following fflush has
         * no effect.  It should be included for
         * portability to other operating systems.
         */



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


***** See Also *****

buffer, fgets(), getc(), STDIO

***** Diagnostics *****

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





COHERENT Lexicon                                           Page 1




gets()                    STDIO Function                   gets()



***** 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 im-
mediately 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
ggeettss  will  receive  is  the  newline  character left  behind  by
ggeettcchhaarr.  A simple statement will remedy this:


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


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






































COHERENT Lexicon                                           Page 2


