gets() function in C
gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store it in a C string. gets() function read the character until a newline character is entered or enter key is entered, or the end of the file is reached.
If we give the string size 15 and the input character are greater than 20, The gets() function will read 20 characters without checking the maximum limit, but while compiling, we get buffer overflow error.
gets() function also reads spaces. The array bound is not checked by the gets() function.
gets() function takes a string as the parameter and returns the string if there are no errors. Else, return a null value. No character given in input at the end of file.
Syntax to call gets() function:
gets(input string name);
Example: gets(string1);
Example program on gets() function:
// program to read and print a string using gets() function.
#include <stdio.h>
int main () // main function
{
char s[50]; // string declaration with size 50.
printf("Enter a string : ");
// Reading the string
gets(s); // function call
printf(" \nThe string which you have entered : %s", s);
return(0);
}
Output:
Enter a string : Hello, Welcome to JavaTpoint tutorials and Examples
The string which you have entered : Hello, Welcome to JavaTpoint tutorials and Examples
Working of gets():
If we want to use gets() function first, include the <stdio.h> header file. Now the program body starts first we declared a string s of size 50.
Next, we gave a printf statement. In the next step, we call the gets function, and it reads the input string till a new line character is entered.
Now the reading part is done, we are printing the input string on the console output screen.
Advantages of gets() function:
- gets() function reads multiple characters at a time.
- Gets() function accepts spaces in the string.
- It takes a single string as a parameter. Ex: gets(str);
Disadvantages of gets() function:
- The gets() function reads the input characters until the new line character key is entered.
- If the input string exceeds the size of the string we get the buffer overflow error.
- We can’t check the bounds of the input array.
- gets() function is not safe.
- Size of the string is fixed.
Simple example program to show buffer overflow error message:
// program to show buffer overflow error message.
#include <stdio.h>
int main () // main function
{
char s[10]; // string declaration with size 10
printf("Enter a string : ");
// Reading the string
gets(s); // function call
printf(" \nThe string which you have entered : %s", s);
return(0);
}
Output:
Enter a string : Welcome to JavaTpoint
*** stack smashing detected ***: terminated
Aborted
Here the size of the given string is greaterthen the given string. The gets() function reads the input successfully, but while returning, it displays *** stack smashing detected ***: terminated Aborted.
Example program to take input string using gets() function and print the length of the string.
// program to calculate the length of the given string using iteration method.
#include <stdio.h>
#include <string.h>
#define max 100
int main () // main function
{
char s[max]; // string declaration.
int i, c=0; // initially length of the string c=0
printf("Enter a string : ");
// Reading the string
gets(s); // function call
printf(" \nThe string you have entered : %s", s);
for(i=0;s[i]!='\0';i++)
{
c++; // incrementing the length of the string for each iteration
}
// printing the length of the string.
printf(" \nThe length of the given string is= %d", c);
return(0);
}
Output:
Enter a string : Hello, Welcome to JavaTpoint tutorials and examples
The string you have entered : Hello, Welcome to JavaTpoint tutorials and examples
The length of the given string is= 51
Example program to read the string using gets() function and display the size of the string using strlen() function.
// program to calculate the length of the given string.
#include <stdio.h>
#include <string.h>
#define max 100
int main () // main function
{
char s[max]; // string declaration.
int a;
printf("Enter a string : ");
// Reading the string
gets(s); // function call
printf(" \nThe string you have entered : %s", s);
a = strlen(s); // calculating the string length using strlen() function
// printing the length of the string.
printf(" \nThe length of the given string is= %d", a);
return(0);
}
Output:
Enter a string : Hello, Welcome to JavaTpoint tutorials and examples
The string you have entered : Hello, Welcome to JavaTpoint tutorials and examples
The length of the given string is= 51
Conclusion:
In this article, we learned how to use the gets() function, its uses, and the advantages and disadvantages of the gets() function.
Examples of the gets() function are also attached to this article. With the help of examples, we can learn the implementation of gets() function using c, and we can identify in which case we can implement this function.