In the loop in the code below, scanf("%[^\n]s",array)
is not working. It does not wait for input and gets skipped. But a space before %
fixes the issue. Why?
Here is the wrong program:
#include <string.h>
#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--){
char arr[199];
scanf("%[^\n]s",arr);
printf("%s",arr);
}
return 0;
}
Here is right code:
#include <string.h>
#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--){
char arr[199];
scanf(" %[^\n]s",arr);
printf("%s",arr);
}
return 0;
}
Why does it need a space before %
for it to work as expected?