Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - gets()

C Library Function - gets()

C Standard Library - <stdio.h> C Standard Library -


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

- Type: `char *` (pointer to 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


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:

  1. #include is the prerequisite for using gets() the prerequisite for using the function.
  2. char str[50]; Define a character array of length 50 to store the input string.
  3. gets(str); Read a line of string from standard input and store it in str it.
  4. printf("您输入的字符串是:%s", str); Use %s Output 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:

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:

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


C Standard Library - <stdio.h> C Standard Library -