Reputation: 809
char middle;
printf("What is your middle name? ");
scanf(" %c", &middle);
printf("Your middle name is %c \n\n", middle);
So I'm trying to make a simple program which ask for a name then prints it out, but it only returns a character, I know that %c is for characters, but what do I have to do to get the full name? Thanks in advance, sorry for such a stupid question, only a beginner :)
Upvotes: 0
Views: 62
Reputation: 9349
char* middle=malloc(EXPECTED_NAME_LENGTH+1);
printf("What is your middle name? ");
scanf(" %s", middle);
printf("Your middle name is %c \n\n", middle);
Upvotes: 1
Reputation: 399919
Reserve space for more than one character:
char middle[32];
and scan and print strings:
if(scanf("%31s", middle) == 1)
printf("Your middle name is %s\n, middle);
Upvotes: 4
Reputation: 6772
You are only getting one char, if you want a complete word, you should use "%s"
as your format string. The following code will get you the middle name as long as you do not type more that 50 characters (try to put more than 50 characters to see what happens!).
char middle[50];
scanf("%s", middle);
Then to print the result:
printf("Middle name is %s", middle);
Upvotes: 1