Namespaces
Variants
Views
Actions

gets, gets_s

From cppreference.com
< c‎ | io
Revision as of 22:36, 31 May 2013 by P12bot (Talk | contribs)

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.

Contents

Parameters

str - character string to be written

Return value

str on success, NULL otherwise

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. Template:rlpt and gets_s() are the recommended replacements.

Never use gets().

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]