gets() Function

The gets() function reads a line from the standard input and stores it as a C string. It reads characters until a newline or end-of-file is encountered and appends a terminating null character at the end. Note that this function is no longer available in C (as of C11) and C++ (as of C++14) due to its inherent risks of buffer overflow.


Syntax of gets()

</>
Copy
char *gets(char *str);

Parameters

ParameterDescription
strA pointer to a character array where the read string will be stored.

It is important to understand that unlike fgets(), the gets() function does not allow you to specify a maximum size for the destination array, which can lead to buffer overflows if the input exceeds the allocated space.

Return Value

On success, the function returns the pointer to the string that was read. If end-of-file is encountered before any characters are read, or if a read error occurs, the function returns a null pointer.


Examples for gets()

Example 1: Basic Usage of gets() to Read a String

This example demonstrates how to use gets() to read a string from standard input and then print it.

Program

</>
Copy
#include <stdio.h>

int main() {
    char input[100];

    printf("Enter a string: ");
    gets(input);  // Reads a line from stdin

    printf("You entered: %s\n", input);
    return 0;
}

Explanation:

  1. A character array input is declared with sufficient space.
  2. The program prompts the user to enter a string.
  3. gets() reads the line from standard input and stores it in input.
  4. The entered string is printed using printf().

Program Output:

main.c:(.text+0x37): warning: the `gets' function is dangerous and should not be used.
Enter a string: Hello World!
You entered: Hello World!

Example 2: Demonstrating gets() Without a Newline

This example illustrates that gets() does not store the newline character in the resulting string.

Program

</>
Copy
#include <stdio.h>

int main() {
    char line[50];

    printf("Type a sentence: ");
    gets(line);

    printf("Output without newline: %s\n", line);
    return 0;
}

Explanation:

  1. A character array line is declared to hold the input.
  2. The program prompts the user to type a sentence.
  3. gets() reads the input until a newline is encountered, but does not store the newline character.
  4. The resulting string is printed.

Program Output:

Type a sentence: This is a test.
Output without newline: This is a test.

Example 3: Handling End-of-File or Read Errors with gets()

This example demonstrates how the return value of gets() can be used to check for end-of-file or read errors.

Program

</>
Copy
#include <stdio.h>

int main() {
    char buffer[80];

    printf("Enter text (Ctrl+D to signal EOF): ");
    if (gets(buffer) == NULL) {
        printf("No input received or an error occurred.\n");
    } else {
        printf("Received: %s\n", buffer);
    }
    return 0;
}

Explanation:

  1. A character array buffer is declared to store the input.
  2. The program prompts the user to enter text and informs how to signal EOF.
  3. gets() is called, and its return value is checked to determine if input was received.
  4. If gets() returns NULL, an error or EOF occurred; otherwise, the received input is printed.

Program Output:

Enter text (Ctrl+D to signal EOF): Sample input
Received: Sample input