Hello there!
I'm having difficulties with understanding what's happening with scanf() in combination with character formatter -%c.
Let's look at simple example:
Code:
#include <stdio.h>
int main(void)
{
char c;
scanf("%c",&c);
scanf("%c",&c);
system("PAUSE");
return 0;
}
Firstly printf() parses all newly typed data, so if I press character 'p' and then enter, the data would be: 'p','\n'.Since I provided character formatter, I'd expect that first character is taken and all additional are removed, but it seems they aren't.Now the interesting part; the 'p' seems to get assigned to variable c and new line-'\n' character is sent to input and waits to be parsed and that happens in second scanf() call.
So what's the problem?Some guys told me that I have to supply a '\n' char in first argument.
Thing worked, but the question is why it worked?From here I can see only a new character added, which should create a new input information(one more loop), but no.Looks like this '\n' character in this context serves as "ignore \n data coming from input" or smth?
And yes, I could use getstr(), but I'm kind of "Always the hard way" guy...