Reading input safely is an essential part of C programming. While functions like gets() exist, they can be dangerous because they don’t check the buffer size. The safer alternative is fgets(), which allows you to read strings from the user or a file safely.
In this tutorial, you’ll learn how fgets() works, its syntax, examples, and outputs.
fgets() is a standard function in <stdio.h> used to read lines of text into a string. It stops reading when:
Because you can specify the buffer size, it prevents buffer overflow—something gets() cannot do.
// Syntax of fgets() in C
char *fgets(char *str, int n, FILE *stream);
Parameters:
Return Value:
// Example: Reading input using fgets()
#include <stdio.h>
int main() {
char name[50];
printf("Enter your name: ");
fgets(name, sizeof(name), stdin);
printf("Hello, %s", name);
return 0;
}
Sample Output 1:
Explanation:
fgets() keeps the newline (\n) if the user presses Enter. To remove it:
// Example: fgets() with newline removal
#include <stdio.h>
#include <string.h>
int main() {
char name[50];
printf("Enter your name: ");
fgets(name, sizeof(name), stdin);
// Remove newline character if present
name[strcspn(name, "\n")] = '\0';
printf("Hello, %s", name);
return 0;
}
Sample Output 2:
Explanation:
// Example: fgets() with limited buffer size
#include <stdio.h>
int main() {
char buffer[10];
printf("Enter a string: ");
fgets(buffer, sizeof(buffer), stdin);
printf("You entered: %s", buffer);
return 0;
}
Sample Output 3:
Enter a string: Hello World
You entered: Hello Wor
Explanation:
// Example: fgets() reading from a file
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
char line[50];
if(file) {
while(fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(file);
} else {
printf("Could not open the file.\n");
}
return 0;
}
Assume example.txt contains:
Hello World
C programming is fun
fgets() is safe
Sample Output 4:
Explanation:
| Feature | gets() | fgets() |
|---|---|---|
| Buffer Control | No control → unsafe | Can set buffer size → safe |
| Newline Handling | Removes newline | Keeps newline |
| Input Source | Keyboard only | Keyboard or file |
| Error Handling | Cannot detect errors | Returns NULL on error or EOF |
| Status | Deprecated in modern C | Recommended and widely used |
Also Read: How to get the length of a string in python
fgets() is a safe, reliable, and flexible way to read strings in C. It works for both keyboard input and file reading, preventing common mistakes like buffer overflow.
By practicing with fgets(), you’ll be able to handle user input safely and write more robust C programs.
gets() is unsafe as it doesn’t check the buffer size, which may cause a buffer overflow. fgets() is safe because you can set the buffer size.
Yes. If there’s enough space in the buffer, fgets() keeps the newline (\n). You can remove it manually using strcspn() or other methods.
Yes. fgets() can read strings both from stdin (keyboard input) and from a file pointer. It’s commonly used to read file data line by line.
fgets() only reads up to n-1 characters and adds a null terminator. Extra characters remain in the input buffer until read again.
Get free consultation for your digital product idea to turn it into reality!
Get Started