gets in c

In this post you will know everything about gets in c that every c programmer should know.

If you dont know about input output in c then read this article.

As we already know gets is a function to read unformatted data from stdin. Data is read from the standard Input device that is the keyboard.

gets is found in stdio header file.

Look at the prototype of gets in c below:

#include<stdio.h>
char * gets ( char * string );


gets in c basic concepts

Lets discuss about the basic concepts about gets function of c programming here.

This function returns a string in which the data read being stored and the parameter of the string type is passed will have the value of the read input from stdin.
String in the sense the character array of C programming language.

This function stops reading from the keyboard when it encounters a new line character.
It even terminates even if you press the enter button after inputting some data.

This function allows you to enter a string including space.

In scanf even though space has a value, if scanf encounters space it ignores input after space if the input is specified as a string. But this function does not ignore the space.

Gets function does not check for the array bound, hence it is risky to use.

If the user inputs more than the allocated string size then it may cause the segmentation fault in the case of Unix Based systems.


gets in c example program

//gets in c
#include<stdio.h>
int main(int argc, char const *argv[])
{
	char s[15];
	gets( s );
	puts(s);
	return 0;
}
gets in c

Differences between scanf and gets in c

Now we will look at the differences between scanf and gets.

Scanf reads the formatted input, where as gets reads the unformatted input.

Format specifier is required for scanf where as no format specifier are required for gets.

Scanf reads until white space is encountered whereas gets reads the input until a new line character was encountered.

In scanf there is less chance of buffer overflow whereas gets have a high chance of buffer overflow if the input exceeds the buffer size.

Look at the table below for simple words.

scanf in cgets in c
Reads the formatted input.Reads the unformatted input.
Format specifier is required.no format specifier are required.
Reads until white space is encountered.Reads the input until new line character was encountered.
There is less chance of buffer overflow.High chance of buffer overflow.

Differences between getc and gets in c

lets look at the differences between getc and gets in c.

Here we have simply ignored the prototype return type of both function.

getc reads a character,where as getc read a string. we already know a string is array of characters. To know more about strings please read this article on strings.

gets may lead to a buffer overflow, but in the case of getc you can check for every character and stop when spaces are encountered.


Now let’s look at the frequently asked questions.

What is puts and gets in C ?

puts is used to output the unformatted string to stdout that is output console.
gets is used to input an unformatted string from the standard input device that is the keyboard.

Both functions are the input output functions of the C standard library stdio.h


What library is gets in C?

gets is in C standard library that is stdio.h .

This is also called as standars input or output header.


Why gets is not working in C?

There are various reasons that gets is not working.

gets in c is so dangerous. hence in some of the libraries it is being removed.

The warning is: the `gets’ function is dangerous and should not be used.

Why gets is not working in C

Still, it is not working check for the header file including a section that is you have to include the standard input-output header as #include<stdio.h>.

While creating this post I could not be able to compare the gets function. I used the 9th standard during compilation I used command as GCC -90 standard as 1990 standard.T

The command is as shown below:

gcc ./gets.c -std=c90 -w -o output
./output

Here I have used -w flag in order to ignore the warnings as gets is too dangerous.

Check this terminal window showing the compilation process.

gets in c warning

Why is gets is dangerous?

If you are wondering Why is it unsafe using gets() in C or Why is get unsafe in C? look at the answer below.

We already know that read until a newline character is encountered. Think of a situation where the user is not going to input the new line character or inputs too much of the data from the standard Input device. In such cases the input buffer we specified goes out of memory hence causes for segmentation fault in UNIX-based systems, in Windows-based systems it may cause abrupt termination.

Consider if you are allocating too much memory simple program, why you have to allocate memory for this reason there is no point of allocating a huge chunk of memory to avoid this problem.

Hence gets in c is completely removed from c11.

If you want to use gets function then you have to use c90 standard.


What to use instead of gets in C?

This question has multiple answer because on which type of input if you want that decides the requirement for the function that you may choose.

You can read a paragraph of string containing space from the scanf. If you don’t know how to please read this post.

You can also check for each character input from the keyboard that it is a white space or end of line or something by using getc.

You can perform the input operation same as gets by using fgets.


Learnig is not over look at the other c programming posts also if you want to be a good c programmer.

Practice more in C programming

Further programming practice

Further practice reading