How to Use gets() Function in C Programming Language

gets() Function in C Programming Language

Introduction :

Using gets() function, you can take string input from the user. The gets() function is used to read a line of text from standard input (keyboard) in the C programming language. In this article, I shall show you how to use gets() function using the C programming language. Here, I also show you the syntax and usage of gets() function.

What is gets() function :

The gets() is a standard input output library (stdio.h) function in C programming language. The gets() function is used to read a string from the keyboard (standard input) and store it into a character array. It reads characters from the keyboard and store in a character (char) type array or buffer. If it encounters a newline character (\n), it stop reading character.

Then, it replaces the newline character (\n) with a null terminator (\0) to make the string. It does not check the size of the array which causes buffer overflow. This can corrupt the input data or crash the program. For this reason, the gets() function is not used in modern C programming language.

Syntax of the gets() function :

The following code is the syntax of gets() function in C programming language.

The parameter of the gets() function is a pointer to an array of characters (string_variable) where the input will be stored. It returns the same pointer to the char array that was passed to it.
It returns NULL if an error or End of File (EOF) occurs.

About the program :

This is a simple program build by C programming language. When you run the program on your PC, it asks you to input a string within 99 characters. After you input a string from the keyboard, it prints the entered string on the console screen.

Explanation of the program :

In the program, you have to include the standard input output library (stdio.h). Now, you can declare a character array “string_variable” of size 100 in the main function. It can hold up to 99 characters and the null terminator (\0). Using printf() function, ask the user to enter a string up to 99 characters. Then, the gets() function reads a line of text from the keyboard and stores it in the “string_variable” array.

Finally, The entered string is displayed on the console using printf() function.

How run the program :

Now, you can install VS Code on your PC to run the program. You can also install Turbo C++ IDE to run the program. After install VS Code, open it and create a new file such as “pac.c”. After that, copy the below code and paste in the “pac.c” file. At last, save the file and run the program on your PC.

Source code of the program :

The following code is used for gets() function in C programming language.

Output of the program :

After running the program, you can see the output of the program on your PC like below image.

output of gets() Function in C Programming Language

Conclusion :

At last, you have learned how to use gets() function in the C programming language. Now you can use gets() function in your own programs. Thank you for visiting my site.

Scroll to Top