Read a String from Console in C

In C, we can read a string from the console using functions such as scanf(), gets() (deprecated), fgets(), and character-by-character input methods like getchar(). The choice of function depends on whether we need to handle spaces, newlines, or buffer overflows. In this tutorial, we will explore different ways to read a string from the console with detailed explanations and examples.


Methods to Read a String in C

1. Reading a String Using scanf()

In this example, we use scanf() to read a string from the console. However, scanf() reads input only until the first whitespace (space, tab, or newline) and cannot read multi-word input.

main.c

</>
Copy
#include <stdio.h>

int main() {
    char name[50];

    // Read a single word using scanf
    printf("Enter your name: ");
    scanf("%s", name);

    printf("Hello, %s!\n", name);
    return 0;
}

Explanation:

  1. We declare a character array name[50] to store the input string.
  2. The printf() function prompts the user to enter a name.
  3. The scanf("%s", name) function reads a single word from user input.
  4. We display the input using printf("Hello, %s!\n", name).

Output:

Enter your name: Arjun
Hello, Arjun!

2. Reading a String Using fgets()

Unlike scanf(), the fgets() function reads a complete line, including spaces, and prevents buffer overflow.

main.c

</>
Copy
#include <stdio.h>

int main() {
    char name[50];

    // Read a line using fgets
    printf("Enter your full name: ");
    fgets(name, sizeof(name), stdin);

    printf("Hello, %s", name);
    return 0;
}

Explanation:

  1. We declare a character array name[50] to store the input string.
  2. The printf() function prompts the user to enter a full name.
  3. The fgets(name, sizeof(name), stdin) function reads a complete line, including spaces, up to 49 characters.
  4. The newline character is also stored in the buffer.

Output:

Enter your full name: Arjun Mitra
Hello, Arjun Mitra

3. Reading a String Character by Character Using getchar()

We can also read a string character by character using getchar(). This method is useful for handling special input conditions.

main.c

</>
Copy
#include <stdio.h>

int main() {
    char name[50];
    int i = 0;
    char ch;

    printf("Enter your name: ");

    // Read characters one by one until newline is encountered
    while ((ch = getchar()) != '\n' && i < 49) {
        name[i++] = ch;
    }
    name[i] = '\0'; // Null-terminate the string

    printf("Hello, %s!\n", name);
    return 0;
}

Explanation:

  1. We declare a character array name[50] and an integer i to track the index.
  2. The while loop reads characters one by one using getchar() until a newline is encountered.
  3. Each character is stored in the name array.
  4. The string is null-terminated manually with name[i] = '\0'.
  5. Finally, we display the name using printf().

Output:

Enter your name: Arjun Apple
Hello, Arjun Apple!

Conclusion

In this tutorial, we explored different ways to read a string from the console in C:

  1. scanf(): Reads a single word but stops at spaces.
  2. fgets(): Reads a full line, including spaces, and is safe against buffer overflows.
  3. getchar(): Reads input character by character, useful for custom parsing.

Using the right function depends on whether you need to handle spaces or limit input size. For safe and multi-word input, fgets() is recommended.