0
  char a[100],b[100],c[100];
  scanf("%[^\n]",a);
  printf("%s",a);
  scanf("%[^\n]",b);
  printf("%s",b);

The compiler seems to be reading the first read but skips the second read. Why is this happening?

metasj
  • 65
  • 1
  • 7
  • 2
    Don't use `scanf`. Use `fgets` and strip of the `\n` if you don't want it – Support Ukraine Aug 06 '18 at 14:49
  • Or, if you want to use `scanf`, you can consume and discard the newline with `scanf("%99[^\n]%*c", a);`. (Added `99` to avoid buffer overflow). – 001 Aug 06 '18 at 14:57
  • 1
    Tip: When I/O is not working as expected, the 1st line of defense coding is to **check the result** value of the functions. `int cnt = scanf("%[^\n]",b); if (cnt != 1) printf("Unexpected scan %d\n", cnt);` would have helped this code a lot to narrow the issue. – chux - Reinstate Monica Aug 06 '18 at 16:06

1 Answers1

1

Because of un handled Enter Use fgets()

Try this :-

char a[100], b[100], c[100];
fgets(a, 100, stdin);
printf("%s", a);
fgets(b, 100, stdin);
printf("%s", b);
anoopknr
  • 3,177
  • 2
  • 23
  • 33