It's not that printf works after fgets , but instead, its output is printed after it.
This is due to the fact that the standard output (the file descriptor that you write with printf ) is line-buffered, that is, the standard library discards prints after the newline character ( \n ) for printing. A.
From man stdout :
The stdout of the stream is buffered by line when it points to the terminal. Partial lines will not be displayed until fflush (3) or exit (3) is called, or a new line is printed.
To examine different results, edit your example to use fflush , or print a standard error using fprintf(stderr, ...
source share