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()
It 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) 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. stdin can be used as argument to read from the standard input. returns : the function returns str
- It follow some parameter such as Maximum length, buffer, input device reference.
- It is safe to use because it checks the array bound.
- It keep on reading until new line character encountered or maximum limit of character array.
Example : Let’s say the maximum number of characters are 15 and input length is greater than 15 but still fgets() will read only 15 character and print it.
// C program to illustrate // fgets() #include <stdio.h> #define MAX 15 int main() { char buf[MAX]; fgets(buf, MAX, stdin); printf("string is: %s\n", buf); return 0; } |
Since fgets() reads input from user, we need to provide input during runtime.
Input: Hello and welcome to GeeksforGeeks Output: 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.
Syntax:
char * gets ( char * str ); str :Pointer to a block of memory (array of char) where the string read is copied as a C string. returns : the function returns str
- It is not safe to use because it does not check the array bound.
- It is used to read string from user until newline character not encountered.
Example : Suppose we have a character array of 15 characters and input is greater than 15 characters, gets() will read all these characters and store them into variable.Since, gets() do not check the maximum limit of input characters, so at any time compiler may return buffer overflow error.
// C program to illustrate // gets() #include <stdio.h> #define MAX 15 int main() { char buf[MAX]; printf("Enter a string: "); gets(buf); printf("string is: %s\n", buf); return 0; } |
Since gets() reads input from user, we need to provide input during runtime.
Input: Hello and welcome to GeeksforGeeks Output: Hello and welcome to GeeksforGeeks
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Problem with scanf() when there is fgets()/gets()/scanf() after it
- Why to use fgets() over scanf() in C?
- Difference between scanf() and gets() in C
- scanf("%[^\n]s", str) Vs gets(str) in C with Examples
- gets() is risky to use!
- Difference Between C Language and LISP Language
- Similarities and Differences between Ruby and C language
- Difference between %d and %i format specifier in C language
- lvalue and rvalue in C language
- Difference between Java and C language
- Difference between while(1) and while(0) in C language
- C Programming Language Standard
- A C Programming Language Puzzle
- C Language Introduction
- Arrays in C Language | Set 2 (Properties)
- Convert C/C++ code to assembly language
- Signals in C language
- Constants vs Variables in C language
- kbhit in C language
- How to use POSIX semaphores in C language
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

