Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is gets() equivalent in C11?

Tags:

From cplusplus.com

The most recent revision of the C standard (2011) has definitively removed this function from its specification

The function is deprecated in C++ (as of 2011 standard, which follows C99+TC3).

I just wanted to know what is the alternative to gets() in C11 standard?

like image 741
Shash Avatar asked Oct 15 '12 10:10

Shash


People also ask

What to use instead of gets ()?

Use fgets() on the stdin stream. Note that unlike gets() , fgets() will not remove the newline character at the end of the input if it will fit in the buffer.

What is the use of gets () function?

The gets() function reads characters from stdin and stores them in str until a newline character or end of file is found. The difference between gets() and fgets() is that gets() uses stdin stream. The gets() function provides no support to prevent buffer overflow if large input string are provided.

What is the header file for gets?

h/gets. gets is a function in the C standard library, declared in the header file stdio. h , that reads a line from the standard input and stores it in a buffer provided by the caller.

Why is fgets better than gets?

fgets() is a safer version of gets() where you can provide limitation on input size. You can also decide to take input from which stream(e.g. File or standard input).


2 Answers

In C11 gets has been substituted by gets_s that has the following declaration:

char *gets_s(char *str, rsize_t n);

This function will read at most n-1 chars from stdin into *str. This is to avoid the buffer overflow vulnerability inherent to gets. The function fgets is also an option. From http://en.cppreference.com/w/c/io/gets:

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().

Given that gets_s is defined in an extension to the standard, only optionally implemented, you should probably write your programs using fgets instead. If you use fgets on stdin your program will also compile in earlier versions of C. But keep in mind the difference in the behavior: when gets_s has read n-1 characters it keeps reading until a new line or end-of-file is reached, discarding the input. So, with gets_s you are always reading an entire line, even if only a part of it can be returned in the input buffer.

like image 113
Joni Avatar answered Oct 26 '22 15:10

Joni


Others have already answered the question. For the sake of completeness, this is the C standard's recommendation:

ISO9899:2011 K.3.5.4.1/6

Recommended practice

The fgets function allows properly-written programs to safely process input lines too long to store in the result array. In general this requires that callers of fgets pay attention to the presence or absence of a new-line character in the result array. Consider using fgets (along with any needed processing based on new-line characters) instead of gets_s.

So you should use fgets whenever possible.

EDIT

gets_s behavior is specified to be:

ISO9899:2011 K.3.5.4.1/4

Description

The gets_s function reads at most one less than the number of characters specified by n from the stream pointed to by stdin, into the array pointed to by s. No additional characters are read after a new-line character (which is discarded) or after end-of-file. The discarded new-line character does not count towards number of characters read. A null character is written immediately after the last character read into the array.

If end-of-file is encountered and no characters have been read into the array, or if a read error occurs during the operation, then s[0] is set to the null character, and the other elements of s take unspecified values.

like image 25
Lundin Avatar answered Oct 26 '22 15:10

Lundin