C Library Function - gets()
In the C language,gets() is a classic function for reading a string from standard input. Although it is simple to use, security issues need to be considered.
gets() The function reads from standard input stdin a line of string and stores it in the specified character array. It reads characters until a newline or end of file is encountered.
Important warning:gets() has a serious security vulnerability: the buffer size cannot be specified, making buffer overflow likely. Therefore, in modern C programming,fgets() is the better choice.
Word meaning:get means "get", ands stands for "string"; together they mean "get string".
Basic syntax and parameters
gets() is a function in the C standard library that requires the header file .
Syntax format
char *gets(char *str);
Parameter Description
- Parameters:
str - Type:
char *(pointer to a character array) - Description: a pointer to a character array used to store the read C string. Ensure the array is large enough to hold the input string.
Function description
- Return Value: if successful, it returns a pointer to
str. If an error occurs or end of file is reached before any characters are read, it returnsNULL. - effect: the read string (excluding the newline) is stored in the array pointed to by
str, and a null character is automatically appended at the end\0as the string terminator.
Example
Let's master it with an example: gets() through a series of examples from simple to complex.
Example 1: Basic usage - reading a string
Example
#include
int
main
(
)
{
char
str
[
50
]
;
printf
(
"Please enter a string: "
)
;
gets
(
str
)
;
printf
(
"The string you entered is: %s"
,
str
)
;
return
(
0
)
;
}
Expected output:
请输入一个字符串:runoob
您输入的字符串是:runoob
Code analysis:
#includeis the prerequisite for usinggets()the prerequisite for using the function.char str[50];Define a character array of length 50 to store the input string.gets(str);Read a line of string from standard input and store it instrit.printf("您输入的字符串是:%s", str);Use%sOutput the string using a format specifier.
Example 2: Handling a NULL return value
In real programming, you should check gets() the return value to handle possible errors.
Example
#include
int
main
(
)
{
char
str
[
100
]
;
char
result
;
printf
(
"Please enter some content (press Ctrl+D to end): \n"
)
;
result
=
gets
(
str
)
;
if
(
result
!=
NULL
)
{
printf
(
"Read successfully, the content is: \n%s\n"
,
str
)
;
}
else
{
printf
(
"Read failed or end of file reached!\n"
)
;
}
return
(
0
)
;
}
Code analysis:
gets()Return a pointer to the same string, or if it fails, returnNULL.- when the user presses Ctrl+D (Linux/Mac) or Ctrl+Z (Windows), the end-of-file condition is triggered,
gets()ReturnNULL. - In real projects, it is recommended to use the safer
fgets()replacesgets().
Example 3: gets() security issue (demonstrating a dangerous operation)
The following example shows why gets() is unsafe.
Example
#include
int
main
(
)
{
// Note: this example is dangerous and for demonstration only!
char
buffer
[
10
]
;
printf
(
"Please enter a long string (more than 10 characters): \n"
)
;
gets
(
buffer
)
;
// No bounds checking, may cause buffer overflow
printf
(
"You entered: %s\n"
,
buffer
)
;
return
(
0
)
;
}
Code analysis:
buffer[10]allocates only 10 bytes of space.- If the user enters a string longer than 9 characters (plus the terminating
\0), aBuffer overflowoccurs, which may overwrite adjacent memory regions. - This can be exploited by attackers to execute malicious code, so
gets()was marked as deprecated in the C99 standard and was removed in the C11 standard.
A better alternative: fgets()
It is strongly recommended to use fgets() instead of gets(), because it can specify the buffer size.
Example
#include
int
main
(
)
{
char
str
[
50
]
;
printf
(
"Please enter a string: "
)
;
// fgets(buffer, buffer size, file stream)
fgets
(
str
,
sizeof
(
str
)
,
stdin
)
;
// Remove the newline read by fgets
// Method: find the newline and replace it with a null character
for
(
int
i
=
0
;
str
[
i
]
!=
'\0'
;
i
++
)
{
if
(
str
[
i
]
==
'\n'
)
{
str
[
i
]
=
'\0'
;
break
;
}
}
printf
(
"The string you entered is: %s\n"
,
str
)
;
return
(
0
)
;
}
fgets() Advantages of
- You can specify the maximum number of characters to read, preventing buffer overflow.
- It keeps the newline at the end of the string (which can be removed manually if needed).
- You can specify the input source (standard input, a file, etc.).
C Standard Library -