Difference between fgets() and gets() in C

1. Introduction

In C programming, fgets() and gets() are functions used to read strings from the input (typically the standard input, like keyboard). However, they have significant differences, particularly in how they handle the length of the input and security.

2. Key Points

1. gets() reads a line from standard input into the buffer until a newline is encountered or end-of-file occurs.

2. fgets() reads a line from the specified input stream into the buffer, but also takes the maximum size of the buffer as an argument.

3. gets() is unsafe and should not be used, as it can cause buffer overflows.

4. fgets() is safer as it limits the number of characters read to prevent buffer overflow.

3. Differences

gets() fgets()
Does not limit the number of characters read, leading to potential buffer overflow. Limits the number of characters read, preventing buffer overflow.
Reads a string from standard input (stdin) only. Can read from any input stream, including files.
No longer recommended and unsafe to use. Recommended for reading lines of text safely.

4. Example

#include <stdio.h>

int main() {
    char buffer[50];

    // Example using fgets()
    printf("Enter text (fgets): ");
    fgets(buffer, 50, stdin);
    printf("You entered: %s\n", buffer);

    // Example using gets() - Unsafe
    // printf("Enter text (gets): ");
    // gets(buffer);
    // printf("You entered: %s\n", buffer);

    return 0;
}

Output:

Enter text (fgets): Hello World
You entered: Hello World
// The output for gets() is commented out due to safety concerns.

Explanation:

1. fgets() reads the input into buffer and stops after 49 characters or when a newline character is encountered.

2. The use of gets() is commented out in the example because it's unsafe and can cause buffer overflow. It doesn't limit the input size.

5. When to use?

- Avoid using gets() due to its inherent security risk. It's generally recommended to use fgets() instead.

- Use fgets() for safely reading lines from a file or standard input, with control over buffer size.

Difference between malloc() and calloc()?

Difference between Local Variable and Global Variable in C

Difference between Global and Static Variables in C

Difference Between Call by Value and Call by Reference in C

Difference Between getch() and getche() in C

Difference between printf() and sprintf() in C

Difference between Arrays and Pointers in C

Difference between Structure and Union in C

Difference Between Stack and Heap Memory Allocation in C

Difference Between Macro and Function in C

Difference between = and == in C

Difference Between for loop and while loop in C

Difference Between Linked List and Array in C

Difference between fgets() and gets() in C

Difference between ++i and i++ in C

Difference between struct and typedef struct in C

Difference between int main() and void main() in C

Difference between Character Array and String in C

Difference between break and continue in C

Difference between exit() and return in C

Comments