0

After printing "Second text", fgets expect from me to enter a string but a program is always being stopped. That happens when I try to enter a char by scanf or getchar. What's happening?

#include <stdio.h>

int main()
{
char c[100],cc;
int x;

printf("First text\n");

scanf("%d",&x);

printf("Second text\n");

fgets(c,100,stdin); 
   //scanf("%c",&cc);
   //cc=getchar();

printf("\n %s %d",c,x);

} 
gaga
  • 13
  • 5

1 Answers1

1

You probably press "enter" after having entered a number; scanf will then read the number, but will leave a '\n' (i.e. the newline represening "enter") in the buffer; This will be treated as an "empty" line then by gets. (BTW: use fgets instead of gets). To overcome this enter the number and the text seperated by a space in a single line (i.e. without a newline in between).

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58