fgets() and gets() in C Programming

Filed Under: C Programming
Gets() And Fgets() In C

Introduction

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:

Problem With Scanf
Problem With scanf()

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() function in C

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:

Use Of Gets()
Use Of Gets

Compare the output with the one while using scanf(). ‘Hello World’ is now treated as a single string.

fgets() function in C

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)

  • str – It is the variable in which the string is going to be stored
  • n – It is the maximum length of the string that should be read
  • stream – It is the filehandle, from where the string is to be read.

Fortunately, we can both read text lines from a file or the standard input stream by using the fgets() function. Let us see how

1. Read from a given file using fgets()

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,

Fgets Output
fgets() file input

2. Read from stdin using fgets()

#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:

Fgets() Stdin Input
fgets() Stdin Input

Conclusion

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.

References

Comments

  1. Adi says:

    Hy, ty for article.

    One comment: “fgets(string,20,fp);” reads the first 19+”\0″ characters, so the result should be: “JournalDev fgets() “.

    Br

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages