gets() — Read a string

Standards

Standards / Extensions C or C++ Dependencies

ISO C
POSIX.1
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3
Language Environment

both  

Format

#include <stdio.h>

char *gets(char *buffer);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <stdio.h>

char *gets_unlocked(char *buffer);

General description

Reads bytes from the standard input stream stdin, and stores them in the array pointed to by buffer. The line consists of all characters up to and including the first newline character (\n) or EOF. The gets() function discards any newline character, and the NULL character (\0) is placed immediately after the last byte read. If there is an error, the value stored in buffer is undefined.

gets() is not supported for files opened with type=record or type=blocked.

gets() has the same restriction as any read operation, such as a read immediately following a write, or a write immediately following a read. Between a write and a subsequent read, there must be an intervening flush or reposition. Between a read and a subsequent write, there must also be an intervening flush or reposition unless an EOF has been reached.

gets_unlocked() is functionally equivalent to gets() with the exception that it is not thread-safe. This function can safely be used in a multithreaded application if and only if it is called while the invoking thread owns the (FILE*) object, as is the case after a successful call to either the flockfile() or ftrylockfile() function.

Returned value

If successful, gets() returns its argument.

If unsuccessful, gets() returns a NULL pointer to indicate an error or an EOF condition with no characters read.

Use ferror() or feof() to determine which of these conditions occurred. Note that EOF is only reached when an attempt is made to read past the last byte of data. Reading up to and including the last byte of data does not turn on the EOF indicator.

Example

CELEBG18
⁄* CELEBG18                                      

   This example gets a line of input from stdin.                                

 *⁄                                                                             
#include <stdio.h>                                                              
#define MAX_LINE 100                                                            
                                                                                
int main(void)                                                                  
{                                                                               
   char line[MAX_LINE];                                                         
   char *result;                                                                
                                                                                
   printf("Enter string:\n");                                                   
   if ((result = gets(line)) != NULL)                                           
      printf("string is %s\n",result);                                          
   else                                                                         
      if (ferror(stdin))                                                        
        printf("Error\n");                                                      
}                                                                               

Related information