The difference between the input functions of scanf of fgets of and gets of in C

  • 2020-06-01 10:31:55
  • OfStack


You know that in the C language, there are three main input functions: scanf(),fgets(), and gets(). Their usage and precautions are as follows:


It is a formatted input method that can enter multiple data fields in a specified format.

The scanf function is a standard library function whose prototype is in the header file "stdio.h". Like the printf function, the C language also allows you to use the stdio.h file without having to include the stdio.h file before using the scanf function.

The general form of scanf function is:

scanf( Format control string ,  Address table column );

The format control string performs the same function as the printf function, but you cannot display a non-format string, which means you cannot display a prompt string. The address table column gives the addresses of the variables. The address is determined by the address operator" & "Followed by the variable name.

Such as: & a, & b represents the address of the variables a and b, respectively.

Let's take a string of the specified length as an example of how to use it and how to easily generate bug.

Requirement: define an array of 10 characters, which is assigned by the user via the keyboard.


  char name[10];


Note: we use %9s because we also need to reserve 1 space for the end of the string symbol '\0', so the maximum number of names the user can enter is 9, and they cannot contain space class characters (space, carriage return, first line indentation), because the scanf function considers them to be the end of the string.

Danger: when the user enters more than 9 pieces of clothing, the system will produce a segment error (segment fault). If you are lucky, the program may not prompt an error. If not, important data in memory may be overwritten!

2.fgets(char *name, int length, fd)

Header file: include < stdio.h >

This function takes the length characters from the file specified by the file descriptor fd and stores them into the length memory units starting at name. Since the maximum number of accepted characters is specified in the parameter, there is no cache overflow problem like the scanf function.

Requirement: define an array of 10 characters, which is assigned by the user via the keyboard.


  char name[10];


Note: because name is the name of an array here, it's essentially a pointer, but the clever sizeof function differentiates it from the 1-like pointer variable to figure out the length of the array based on the previous definition. But if it's a pointer like a 1, sizeof is going to get a 4 or an 8.


It gets string input from the keyboard, and there are no string length restrictions or checks. It is recommended that we avoid using this function in our code!

Requirement: define an array of 10 characters, which is assigned by the user via the keyboard.


  char name[10];


Note: this function can read indefinitely and does not determine the upper limit, so the programmer should make sure that the buffer is large enough that it does not overflow when performing a read. To avoid this, we can replace gets () with fgets().

Danger: avoid using this function at all times, even though it has been around for a long time.

The difference between:

1. Is there a limit on the number of characters the user can enter?

Both are limited, but the scanf function requires you to indicate them in the formatting specifier. The fgets function enforces it as a parameter.

2. Can users enter data from multiple domains at the same time?

While the scanf function can implement multifield input by adding multiple formatted input specifiers, the fgets function can assign values to only one field at a time.

3. Can the string entered by the user contain space class characters?

The scanf function takes a space character as the end of a string, so you can't have Spaces in a string. Otherwise you can.


Related articles: