C Programming

Fgets() Function in C

The title of this article little clears out the purpose of using the fgets function in C. The fgets() function in C is mainly designed to get the data input from a user or an input stream like file and display it within the output console. To get the input from a user to display on the console, we must have some buffer memory or array in which we can store that input. Using this function, we can restrict the number of characters to be displayed from an input stream and avoid displaying the excess data and display only the needed ones. This guide covers some C examples to explain the use of the fgets() function in detail.

The system update is a must before performing any sort of coding on it as it caters to all the memory-related issues and makes your system feature full. Thus, the “update” keyword with the “apt” utility and “sudo” privilege is a must. After adding this query to your Linux shell, it would require the passcode of a currently logged-in user.

If the Linux system already has a C compiler configured in it, you don’t need to add it again. If it’s not configured, you will face problems while executing the code. Thus, install it by utilizing the “apt” utility within the “install” instruction followed by the “gcc” utility name.

Before having a clear look at the C example for the fgets function, we have to create a “c” file. The “fgets.c” file that we create can be seen in the list of files and folders of the current “home” directory after using the “ls” query after creating it with the “touch” query.

After the file has been successfully created, we are going to open it in Linux’s “GNU Nano” editor. Therefore, we tried the displayed command in the shell.

Example 1:

We perform the first example of C to utilize the fgets function to get the input data from the user at run time and display a specific range of characters from it on the shell. Here is the C code that is used to get the data from a user. This code uses the standard input/output header for the use of the standard input and output streams like stdio.h. Before using the main() function, we define a MAX variable with the value 20 that is used as a range. The main() method is used here for the overall functionality.

The character type array “A” of size “MAX” is declared and the printf() function of C is used to display the “Input: ” string on the shell to ask the user for an input. The fgets() function of C is called here by passing it an “A” array, MAX variable, and “stdin” standard input output mutable as arguments to get the input from a user. This input is saved to the “A” array up to “MAX” length. A total of 20 characters is stored and the rest is discarded.

The printf() statement then uses the “A” array to display those 20 characters from the input. The return 0 statement ends the program smoothly after the execution. Save it before this code execution.

#include <stdio.h>

#define MAX 20

int main() {

  char A[MAX];

printf("Input: ");

fgets(A, MAX, stdin);

printf("Data: %s\n", A);

return 0;


After saving our C fgets() function code to its file, we exit the “nano” editor with the “Ctrl+X” and apply the “gcc” command to the “fgets.c” file to make it compiled. Nothing happened, so we tried the “./a.out” query to run the program. The “Input” string appeared along with the text area to get an input from us.

We add a single line of more than 30 characters and hard-press the “Enter” key. The inputted data is displayed up to the first 20 characters and the rest is discarded.

Example 2:

Within our new C illustration, we will demonstrate the use of the fgets() function to get the text data from the input file stream and display it on the console. Therefore, the main structure of this code is very similar to the previous example. The main() function starts with the declaration of a file descriptor “f” of pointer type by utilizing the built-in FILE object. The character type array “A” of size 30 is declared and the fopen() function of C is called to read the input stream “fgets.txt” from the system.

The returned value is saved to the file descriptor NULL. The “if” statement checks if the value of “f” is NULL or something else. If the value is “NULL”, it throws an error using the “perror” function of C. Otherwise, the “else-if” part of the statement is executed which is called the “fgets” function.

The purpose of using the fgets() method is to get 30 characters from an input stream “f”, save them to the “A” array, and check if the input is not NULL. When the whole input is not NULL, the puts() function of C is called here to display the whole input of 30 characters at the console while passing it the “A” array as an argument. The stream is closed using the fclose() function.

 f = fopen("fgets.txt", "r");
if(f == NULL) {
return(-1); }
 else if(fgets(A, 30, f)!=NULL) {
    puts(A); }
 return 0;

Before compiling our code, we show you the text data within the fgets.txt file (that is used as an input stream) with the “cat” display query. It shows the 2 lines of the text data. Now, the compilation and execution take place. We get a single first line at the output area. This is because we choose to only get 30 characters from the input file stream.


While concluding this article, we are a hundred percent sure that you will not regret taking the help from our article when you want to learn some basics about the fgets() function of C. We discussed about how you can utilize this function to get the standard input from the user at runtime and how you can get the data from a file stream and display it in the output area.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content