read a document in c getting name from console
To read a document in C and get a name from the console, you can use the scanf
function to read input from the user. Here's an example code snippet:
#include <stdio.h>
int main() {
char name[50];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s!\n", name);
return 0;
}
In this code, we declare a character array name
with a size of 50 to store the name entered by the user. Then, we use printf
to prompt the user to enter their name. The %s
format specifier is used with scanf
to read a string (sequence of characters) from the user and store it in the name
array. Finally, we use printf
to display a greeting message that includes the entered name.
Note that in this example, the assumption is that the user will enter a single word as the name. If you want to read a full name with spaces, you can use the %[^\n]
format specifier with scanf
instead, like this:
scanf(" %[^\n]", name);
This will read the entire line of input until a newline character (\n
) is encountered, allowing you to capture a name with spaces.
Remember to include the necessary header file stdio.h
for input/output operations, and to compile and run the code to see the desired output.