We all are familiar with the scanf()
function. It is the main function applicable to take basic user inputs. Even though scanf()
works great while taking inputs such as integer, character, float etc. It certainly falls behind while taking string inputs containing whitespaces. Let’s take a look at an example,
#include<stdio.h>
int main()
{
char string[10];
printf("Enter the string: ");
scanf("%s", string);
printf("\n %s",string);
return 0;
}
Output:
As we can observe from the above example. scanf()
stops scanning as soon as it encounters whitespace or newline. This, in fact, makes taking string inputs using scanf()
a bit troublesome. This can be easily avoided by using some other input functions like gets()
and fgets()
.
In this article, we are going to learn how to apply both the functions and compare them side by side.
gets()
is a pre-defined function in C which is used to read a string or a text line. And store the input in a well-defined string variable. The function terminates its reading session as soon as it encounters a newline character.
Syntax:
gets( variable name );
The given code below illustrates the use of the gets()
function,
#include<stdio.h>
int main()
{
char string[10];
printf("Enter the String: ");
gets(string);
printf("\n%s",string);
return 0;
}
Output:
Compare the output with the one while using scanf()
. ‘Hello World’ is now treated as a single string.
The standard C library also provides us with yet another function, the fgets()
function. The function reads a text line or a string from the specified file or console. And then stores it to the respective string variable.
Similar to the gets()
function, fgets also terminates reading whenever it encounters a newline character. But furthermore, unlike gets()
, the function also stops when EOF is reached or even if the string length exceeds the specified limit, n-1.
Syntax,
fgets(char *str, int n, FILE *stream)
Fortunately, we can both read text lines from a file or the standard input stream by using the fgets()
function. Let us see how
For example,
#include<stdio.h>
int main()
{
char string[20];
FILE *fp;
fp=fopen("file.txt","r");
fgets(string,20,fp);
printf("The string is: %s",string);
fclose(fp);
return 0;
}
Consider file.txt to contain the line ‘JournalDev fgets() example!’. In that case, the output of the above code would be,
#include<stdio.h>
int main()
{
char string[20];
printf("Enter the string: ");
fgets(string,20,stdin); #input from stdin stream
printf("\nThe string is: %s",string);
return 0;
}
Output:
Even though both the functions, gets()
and fgets()
can be used for reading string inputs. The biggest difference between the two is the fact that the latter allows the user to specify the buffer size. Hence it is highly recommended over the gets()
function.
The gets()
function doesn’t have the provision for the case if the input is larger than the buffer. As a result, memory clogging may occur. This is the part where the fgets()
function shines and provides an ultimate solution.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Hy, ty for article. One comment: “fgets(string,20,fp);” reads the first 19+“\0” characters, so the result should be: "JournalDev fgets() ". Br
- Adi