Why does spacing matter in this context?

bnd

Joined
Aug 1, 2022
Messages
1
Reaction score
0
Hi,

I'm still in the beginning stages of learning C so please bear with me.

I was wondering why my code would not read the user input for a string.

My code consisted of the following:

int main (void){

char userChar;
char userString[50];


printf("Enter character:\n");
scanf("%c", &userChar); //assigning input character//

printf("Enter string:\n");
scanf("%s", userString); //assigning input string//


printf("%c %s", userChar, userString);


return 0;
}


I wasn't getting an output for the userString unless I put a space in front of the %c for the scanf function, like so:

scanf(" %c", &userChar);
^

Only then would the userString print.

Would anyone be able to tell me what difference that little space makes???
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top