I am frequently running into unexpected behavior while using the scanf() function in C. Especially with character variables.
For example:
int main()
{
char c1, c2;
int no;
printf("Enter c1: ");
scanf("%c", &c1);
printf("\nEnter c2: ");
scanf("%c", &c2);
printf("\nEnter age: ");
scanf("%d", &no);
printf("%c %d ", c1, no);
printf("-%c-", c2);
return 0;
}
Here, it seems, c2 is automatically taking 'new line' as the input.
I frequently run into errors of similar kinds with scanf(). Could someone explain what is happening here?
I don't know much C so please suggest good programming practices when using scanf() function to escape from such errors.
Since this is my first question on StackOverflow please forgive any inadvertent mistakes on my part :).
(I am not sure if this has already been answered before....)