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
#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:
- We declare a character array
name[50]to store the input string. - The
printf()function prompts the user to enter a name. - The
scanf("%s", name)function reads a single word from user input. - 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
#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:
- We declare a character array
name[50]to store the input string. - The
printf()function prompts the user to enter a full name. - The
fgets(name, sizeof(name), stdin)function reads a complete line, including spaces, up to 49 characters. - 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
#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:
- We declare a character array
name[50]and an integerito track the index. - The
whileloop reads characters one by one usinggetchar()until a newline is encountered. - Each character is stored in the
namearray. - The string is null-terminated manually with
name[i] = '\0'. - 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:
scanf(): Reads a single word but stops at spaces.fgets(): Reads a full line, including spaces, and is safe against buffer overflows.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.
