#include <stdio.h>
#include <string.h>
#include<stdio.h>
int main()
{
int ch;
char str;
scanf("%d", &ch);
scanf("%c", &str);
printf("x = %d, str = %c", ch, str);
return 0;
}
Input: 10(enter)
Output: x = 10, str =
here in this code scanf("%d", &ch); reads an integer and leaves a newline character in buffer. So scanf("%c", &str); only reads a newline character. Which i understood.
But when i run this code:
#include <stdio.h>
#include <string.h>
#include<stdio.h>
int main()
{
int ch;
char str[54];
scanf("%d", &ch);
scanf("%s",str);
printf("x = %d, str = %s", ch, str);
return 0;
}
Input: 10(enter) test
Output: x = 10, str = test
here it seems like scanf("%s",str); ignores the newline character from the buffer and reads test from console.
Why this is happening? Give me a proper detailed Explanation