Defined in header
char *gets( char *str );
|(removed in C11)|
char *gets_s( char *str, rsize_t n );
| (since C11)
struntil a newline character is found or end-of-file occurs. A null character is written immediately after the last character read into the array. The newline character is discarded but not stored in the buffer.
str, and always writes the terminating null character (unless str is a null pointer). The newline character, if found, is discarded and does not count toward the number of characters written to the buffer.
- The following errors are detected at runtime and call the currently installed constraint handler function:
nis greater than RSIZE_MAX
stris a null pointer
- endline or eof not encountered after storing n-1 characters to the buffer.
- In any case,
gets_sfirst finishes reading and discarding the characters from stdin until new-line character, end-of-file condition, or read error before calling the constraint handler.
- As all bounds-checked functions,
gets_sis only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including
|str||-||character string to be written|
 Return value
str on success, NULL on failure.
If the failure has been caused by end of file condition, additionally sets the eof indicator (see feof()) on stdin. If the failure has been caused by some other error, sets the error indicator (see ferror()) on stdin.
gets() function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear on
stdin). For this reason, the function has been deprecated in the third corrigendum to the C99 standard and removed altogether in the C11 standard. fgets() and
gets_s() are the recommended replacements.
- C11 standard (ISO/IEC 9899:2011):
- K.22.214.171.124 The gets_s function (p: 602-603)
- C99 standard (ISO/IEC 9899:1999):
- 126.96.36.199 The gets function (p: 298)
- C89/C90 standard (ISO/IEC 9899:1990):
- 188.8.131.52 The gets function
 See also
| reads formatted input from stdin, a file stream or a buffer |
| gets a character string from a file stream |
| writes a character string to a file stream |
(dynamic memory TR)
| read from a stream into a automatically resized buffer until delimiter/end of line |
C++ documentation for gets