Table of contents
1.
Introduction
2.
fgets()
3.
gets()
4.
How fgets() and gets() compare to each other?
5.
Frequently Asked Questions
5.1.
What isDifference between  fgets() and gets() in C?
5.2.
Which is safe  fgets() and gets() in C?
5.3.
What does the fgets () function do?
5.4.
What does the gets () function do?
5.5.
How many arguments does fgets() take?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

fgets() and gets() in C

Author Alok Pandey
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

We've all heard of the scanf() function. It is the primary function for taking basic user inputs. Although scanf() works well with various inputs such as integers, characters, and floats, When taking string inputs with whitespaces, it lags.

When scanf() comes across whitespace or a newline, it stops scanning. In reality, using scanf() to take string inputs is a bit of a pain. Other input functions such as gets() and fgets can be used to avoid this ().Now we are going to discuss fgets() and gets() in C in details.

Also see: C Static Function, and  Tribonacci Series

fgets()

The function reads characters from the file until it encounters a newline  ('\n'), n-1 characters, or the end of the file, whichever comes first. To read a string from a file, use fgets(). 

It accepts three input arguments and stores them in a null-terminated array. When it comes to arguments, the first is the storage array in which we want our string to be stored. The file reference is the third, and the count of variables we want to obtain from the files is the second.It returns a pointer to str if it succeeds. It returns NULL in the event of an error or the end of the file.

Syntax

fgets(char *str, int n, FILE *stream);

 

Code

#include<stdio.h>
int main()
{
    FILE *fp;    
    char string[20];
    fp=fopen("data.txt","r");
    fgets(string,20,fp);
    printf("The string is: %s",string);
    fclose(fp);
    return 0;
}

 

Output

The string is : Coding ninjas is best!

gets()

The gets() function takes a line from stdin and puts it in the buffer. All characters do into the buffer up to but not including the initial new-line character (\n) or the end-of-file (EOF). If a new-line character is read, the gets() function replaces it with a null character (\0) before returning the line.

Syntax

gets(variable name );

 

Code

#include<stdio.h>
int main()
{
    char string[15];
    printf("Kindly Input the text : ");
    gets(string);
    printf("\n%s",string);
    return 0;
}

 

Output

Kindly Input the text : Hello world
Hello world

You can also read about dynamic array in c and Short int in C Programming

Must Read Passing Arrays to Function in C

How fgets() and gets() compare to each other?

gets() and fgets() are C functions that accept a string with spaces between characters as input. The issue with gets() is that it has a buffer overflow, which means it takes more input than it should. fgets is used to solve this problem ().

The difficulty with the most widely used scanf() function is that it cannot accept spaces in a string's input. When we take a string in a buffer for standard input or a file, we use the gets() and fgets() functions.

Check out also “strcat() function in C

Must Read what is storage class in c and Decision Making in C

Frequently Asked Questions

What isDifference between  fgets() and gets() in C?

When a function gets() reads the input from standard input, it converts the newline ('\n') to the null character ('\0'). On the other hand, when function fgets() reads a newline ('\n') character from the file, it doesn't convert it into a null character ('\0'). It is retained as it is.

Which is safe  fgets() and gets() in C?

Compared to gets, fgets is safer to use because it checks for character array str limits. Till a newline character is met, gets continues to read characters from the users.

What does the fgets () function do?

To read a string from a file, use fgets(). It accepts three input arguments and stores them in a null-terminated array.

What does the gets () function do?

The gets() function takes a line from stdin and puts it in the buffer. All characters do into the buffer up to but not including the initial newline character (\n) or the end-of-file (EOF).

How many arguments does fgets() take?

It accepts three input arguments and stores them in a null-terminated array. Regarding arguments, the first is the storage array in which we want our string to be stored.

Conclusion

In this blog, we have discussed the function fgets() and gets() in C.

We hope that this blog has helped you enhance your knowledge regarding  the function fgets() and gets() in C

and if you would like to learn more, check out our articles on 

 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. 

Enroll in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass