I am trying to learn programming and was trying the following code in C. However, As you can see below I am not getting the expected output and I am getting a segmentation error while trying out solutions :
I feel I am doing something wrong with the declaration of First name or something. But not able to figure it out.
My Code:
#include <stdio.h>
int main(void){
char first_name;
printf("Enter your name: \n");
scanf("%c", &first_name);
printf("Hello, %c", first_name);
}
Actual Output
This is the output I am currently getting:
Enter your name:
asdasfasf
Hello, a
Expected Output
Enter your name:
asdasfasf
Hello, asdasfasf
>Solution :
Here is my answer
#include <stdio.h>
int main(void){
char first_name[100] = {0};
printf("Enter your name:(Max length is 99)\n");
scanf("%99s", first_name);
printf("Hello, %s\n", first_name);
}
Run it will output
Enter your name:(Max length is 99)
asdasfasf
Hello, asdasfasf