how to store a user input with spaces in c
To store user input with spaces in C, you can use the fgets
function. Here's an example:
#include <stdio.h>
int main() {
char input[100];
printf("Enter your input: ");
fgets(input, sizeof(input), stdin);
printf("You entered: %s", input);
return 0;
}
In this example, the fgets
function is used to read the user input. The input is stored in the input
character array, which has a size of 100 characters. The sizeof(input)
argument specifies the maximum number of characters to read, preventing buffer overflow.
The stdin
argument to fgets
indicates that the input should be read from the standard input, which is usually the keyboard.
After reading the input, you can manipulate or process it as needed. In this example, the input is simply printed back to the user.
Remember to include the <stdio.h>
header file at the beginning of your program to use the fgets
and printf
functions.