Namespaces
Variants
Views
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)

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.

Contents

[edit] Parameters

str - character string to be written

[edit] Return value

str on success, NULL otherwise

[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

reads formatted input from stdin, a file stream or a buffer
(function) [edit]
gets a character string from a file stream
(function) [edit]
writes a character string to a file stream
(function) [edit]