C – Read String using Scanf()
To read a string entered by user via standard input in C language, use scanf() function.
scanf() reads input from stdin(standard input), according to the given format and stores the data in the given arguments.
So, to read a string from console, give the format and argument to scanf() function as shown in the following code snippet.
</>
                        Copy
                        char name[30];
scanf("%s", name);Here, %s is the format to read a string and this string is stored in variable name.
Examples
In the following example, we read a string input by the user in standard input to a variable name using scanf() function.
main.c
</>
                        Copy
                        #include <stdio.h>
int main() {
    char name[30];
    printf("Enter your name : ");
    //read input from user to "name" variable
    scanf("%s", name);
    //print to console
    printf("Hello %s!\n", name);
    return 0;
}Output
Enter your name : Arjun
Hello Arjun!
Program ended with exit code: 0Enter your name : Nemo
Hello Nemo!
Program ended with exit code: 0Conclusion
In this C Tutorial, we learned how to read a string entered by user via standard input using scanf() function.
