The Wayback Machine - https://web.archive.org/web/20240727142635/https://www.geeksforgeeks.org/fgets-gets-c-language/
Open In App

fgets() and gets() in C language

Last Updated : 06 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

For reading a string value with spaces, we can use either gets() or fgets() in C programming language. Here, we will see what is the difference between gets() and fgets().

fgets()

The fgets() reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first.

Syntax

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

Parameters

  • str: Pointer to an array of chars where the string read is copied.
  • n: Maximum number of characters to be copied into str (including the terminating null character).
  • *stream: Pointer to a FILE object that identifies an input stream.

Note: stdin can be used as argument to read from the standard input.

Return Value

  • The fgets() function returns a pointer to the string where the input is stored.

Features of fgets()

  • It follows some parameters such as Maximum length, buffer, and input device reference.
  • It is safe to use because it checks the array bound.
  • It keeps on reading until a new line character is encountered or the maximum limit of the character array.

Example of fgets()

Let’s say the maximum number of characters is 15 and the input length is greater than 15 but still fgets() will read only 15 characters and print it. 

C




// C program to illustrate fgets()
#include <stdio.h>
#define MAX 15
 
int main()
{
    // defining buffer
    char buf[MAX];
 
    // using fgets to take input from stdin
    fgets(buf, MAX, stdin);
    printf("string is: %s\n", buf);
 
    return 0;
}


Since fgets() reads input from the user, we need to provide input during runtime.

Input:
Hello and welcome to GeeksforGeeks

Output:
string is: Hello and welc

gets()

Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached.

  • It is not safe to use because it does not check the array bound.
  • It is used to read strings from the user until a newline character is not encountered.

Syntax

char *gets( char *str );

Parameters

  • str: Pointer to a block of memory (array of char) where the string read is copied as a C string.

Return Value

  • The function returns a pointer to the string where input is stored.

Example of gets()

Suppose we have a character array of 15 characters and the input is greater than 15 characters, gets() will read all these characters and store them into a variable. Since, gets() does not check the maximum limit of input characters, at any time compiler may return buffer overflow error. 

C++




// C program to illustrate
// gets()
#include <stdio.h>
#define MAX 15
 
int main()
{
    // defining buffer
    char buf[MAX];
 
    printf("Enter a string: ");
 
    // using gets to take string from stdin
    gets(buf);
    printf("string is: %s\n", buf);
 
    return 0;
}


Since gets() reads input from the user, we need to provide input during runtime.

Input:
Hello and welcome to GeeksforGeeks

Output:
Hello and welcome to GeeksforGeeks

Conclusion

Both fgets() and gets() functions can be used for reading string input from standard input. The main difference between fgets() function and gets() function is that fgets() function allows the user to specify the maximum number of characters to read and we can also change the input stream to any file in fgets().



Previous Article
Next Article

Similar Reads

Problem With Using fgets()/gets()/scanf() After scanf() in C
scanf() is a library function in C. It reads standard input from stdin. fgets() is a library function in C. It reads a line from the specified stream and stores it into the string pointed to by the string variable. It only terminates when either: end-of-file is reachedn-1 characters are readthe newline character is read 1) Consider a below simple p
3 min read
Difference between scanf() and gets() in C
scanf()It is used to read the input(character, string, numeric data) from the standard input(keyboard).It is used to read the input until it encounters a whitespace, newline or End Of File(EOF). For example see the following code: C/C++ Code // C program to see how scanf() // stops reading input after whitespaces #include &amp;lt;stdio.h&amp;gt; in
3 min read
gets() is risky to use!
Consider the below program. C/C++ Code void read() { char str[20]; gets(str); printf(&quot;%s&quot;, str); return; } The code looks simple, it reads string from standard input and prints the entered string, but it suffers from Buffer Overflow as gets() doesn't do any array bound testing. gets() keeps on reading until it sees a newline character. To
2 min read
scanf("%[^\n]s", str) Vs gets(str) in C with Examples
gets()gets is a more convenient method of reading a string of text containing whitespaces.Unlike scanf(), it does not skip whitespaces.It is used to read the input until it encounters a new line.%[^\n]It is an edit conversion code.The edit conversion code %[^\n] can be used as an alternative to gets.C supports this format specification with scanf()
2 min read
Difference Between C Language and LISP Language
C Language: C is the procedural Programming language. It was designed to be compiled using a compiler. The Language has small and fixed number of keywords like if/else, for, while,.. etc. We can use more than one assignment that may be used in one statement in this language. Functions are also used here, it can return values that can be ignored, wh
2 min read
Similarities and Differences between Ruby and C language
Similarities between Ruby and C There are many similarities between C and Ruby, and some of them are: Like C, in Ruby also… A programmer is able to program procedurally if they like to do. But still, behind the scenes, it will be object-oriented.Both the languages have the same operators, for example, compound assignment and bitwise operators. But
3 min read
Difference between Java and C language
Here are some of the differences between Java and C language. C is much faster than Java Java is slower than C due to overhead. C Java C was developed by Dennis M. Ritchie between 1969 and 1973.Java was developed by James Gosling in 1995.C is a Procedural Programming Language.Java is Object-Oriented language.C is more procedure-oriented.Java is mor
3 min read
Difference between %d and %i format specifier in C language
A format specifier is a special character or sequence of characters used to define the type of data to be printed on the screen or the type of data to be scanned from standard input. A format specifier begins with a '%' character followed by the sequence of characters for different types of data. In short, it tells us which type of data to store an
3 min read
lvalue and rvalue in C language
lvalue:- lvalue simply means an object that has an identifiable location in memory (i.e. having an address). In any assignment statement "lvalue" must have the capability to store the data.lvalue cannot be a function, expression (like a+b) or a constant (like 3 , 4 , etc.). L-value: "l-value" refers to memory location which identifies an object. l-
4 min read
Difference between while(1) and while(0) in C language
Prerequisite: while loop in C/C++ In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The boolean condition is either true or false. while(1) It is an infinite loop which will run till a break statement is issued explicitly. Interestingly not
3 min read
Practice Tags :
three90RightbarBannerImg