Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue in C language using 'fgets' after 'printf' as 'fgets' runs before 'printf' [duplicate]

Tags:

c

printf

fgets

Possible Duplicate:
Why does printf not flush after the call unless a newline is in the format string? (in C)

I am getting a problem using printf and fgets as in my code printf is written earlier then fget but it does not run, it runs after fgets runs.

enum { max_string = 127 };
static char string[max_string+1] = "";

int main( int argc, char ** argv ) {    
      printf("Type a String: ");
      fgets(string, max_string, stdin);
      printf("The String is %s\n", string);
      return 0;
}
like image 530
Tanveer Singh Avatar asked Jul 20 '12 07:07

Tanveer Singh


1 Answers

do a flush of the stdout

fflush(stdout);

before fgets(...)

printf("Type a String: ");  
fflush(stdout);
fgets(string, max_string, stdin); 
like image 68
AndersK Avatar answered Oct 13 '22 10:10

AndersK