How to use C library function gets() with example:
The gets() function is a function used to take user-inputs. It is defined in the <stdio.h> header file. In this post, we will learn how to use the gets() function with examples.
Definition of gets:
The gets() function is defined as like below:
char* gets(char *s)
It reads the input from the standard input and stores that in s. It stops if a new line or end-of-file is reached.
It will not copy the new line but it will add a terminating null character to the end of s.
Return value of gets:
On success, gets() function returns the C string s. It returns NULL if any error is encountered.
Example of gets:
Let’s take an example of gets:
#include <stdio.h>
int main()
{
char s[20];
printf("Enter a string: ");
gets(s);
printf("You have entered: %s\n", s);
}
In this program,
- s is a character array to hold the user input string.
- We are using printf to ask the user to enter a string.
- gets put the string in s
- The last line is printing the user input string.
If you run this program, it will print output as like below:
Enter a string: Hello World!!
You have entered: Hello World!!
warning: this program uses gets(), which is unsafe
If you run the above program, it will throw a warning message as like below:
warning: this program uses gets(), which is unsafe
This error is thrown because gets doesn’t checks the bound i.e. it doesn’t verify if the user input string can be stored in the storage provided or not. It may throw buffer overflow if the input string is larger than the storage.
This is a limitation of gets.
Difference between gets and fgets:
gets is different than fgets.
- It uses stdin as the source
- It doesn’t add the ending newline character in the final result string.
- It doesn’t provide any way to specify the maximum length of a string and it might lead to buffer overflow.
Can I use gets() in C++:
You can use gets() function in C++ as well. You need to include the below import statement:
#include <cstdio>
Difference between gets() and scanf():
Both gets() and scanf() methods can be used to get user inputs. But there is a difference between these two: scanf() reads the input until it encounters a space. But gets() will read the whole sentence until a new line is found.
Let’s try it with an example:
#include <stdio.h>
int main()
{
char str[20];
int type;
printf("Enter 1 to use scanf and any other number to use gets: ");
scanf("%d", &type);
getchar();
printf("Enter a string: ");
if (type == 1)
{
scanf("%s", str);
}
else
{
gets(str);
}
printf("You have entered: %s\n", str);
}
In this program,
- str is the character array to hold the user input string.
- type is an integer variable to hold a type. If type is 1, it will use scanf to read the user input. For any other value of type, it will use gets.
- It reads the user input string by using scanf and gets and prints the final string value str.
If you run this above program, it will print output as like below:
Enter 1 to use scanf and any other number to use gets: 2
warning: this program uses gets(), which is unsafe.
Enter a string: hello world
You have entered: hello world
Enter 1 to use scanf and any other number to use gets: 1
Enter a string: hello world
You have entered: hello
You might also like:
- C program to check if a number is Abundant or Excessive
- C program to find if a number is a Deficient number or not
- C program isxdigit method example to check if a character is hexadecimal
- C program to find all Abundant numbers from 1 to 100
- C program to check if a character is lowercase using islower
- C program to check if a character is uppercase using isupper
- C program to print all Deficient numbers from 1 to 100
- C program to find all disarium numbers from 1 to 100
- C program to check if a number is a Kaprekar number or not