gets, gets_s - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# gets, gets_s

From cppreference.com
< c‎ | io

Defined in header `<stdio.h>` |  |

char *gets( char *str ); |  | (until C11)
char *gets_s( char *str, rsize_t n ); |  | (since C11) (optional)

1) Reads stdin into the character array pointed to by `str` until a newline character is found or end-of-file occurs. A null character is written immediately after the last character read into the array.
2) Reads at most n-1 characters from stdin into the array pointed to by `str` until new-line character, end-of-file condition, or read error. A null character is written immediately after the last character read into the array, or to str[0] if no characters were read.
If `n` is zero or is greater than RSIZE_MAX, a null character is written to str[0] but the function reads and discards characters from stdin until new-line character, end-of-file condition, or read error.
If `n-1` characters have been read, continues reading and discarding the characters from stdin until new-line character, end-of-file condition, or read error.
The `gets_s` function is an extension and is optional. It is guaranteed to be present only if __STDC_LIB_EXT1__ is defined.

### [edit] Parameters

str | - | character string to be written

### [edit] 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.

### [edit] Notes

The `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.

Never use `gets()`.

### [edit] See also

scanffscanfsscanf | reads formatted input from stdin, a file stream or a buffer (function) [edit]
fgets | gets a character string from a file stream (function) [edit]
fputs | writes a character string to a file stream (function) [edit]
C++ documentation for gets

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=c/io/gets&oldid=66060"