Confusing Output From C Program

  • A+

I have a C program that compiles to an executable called myprogram. This is its main function:

int main(int argc, char ** argv) {   printf("this is a test message./n");   system("ls");    return 0; } 

When I run myprogram > output.txt in a Linux shell and then examine output.txt, I see the output of ls listed above "this is a test message."

I feel like it should be the other way around. Why is this happening, and what can I do so that "this is a test message" appears at the top of output.txt?

If it matters, I'm new to both C and working in a command line.


By default output to stdout is line-buffered. That is, the buffer is flushed when it's full or when you add a newline.

However that's the default, when stdout is connected to a terminal. If stdout is not connected to a terminal, like what happens when you redirect the output from your program, then stdout becomes fully buffered. That means the buffer will be flushed and actually written either when it's full or when explicitly flushed (which happens when the program exits).

This means that the output of a separate process started from your code (like what happens when you call system) will most likely be written first, since the buffer of that process will be flushed when that process ends, which is before your own process.

What happens when using redirection (or pipes for that matter):

  1. Your printf call writes to the stdout buffer.
  2. The system function starts a new process, which writes to its own buffer.
  3. When the external process (started by your system call) exits, its buffer is flushed and written. Your own buffer in your own process, isn't touched.
  4. Your own process ends, and your stdout buffer is flushed and written.

To get the output in the "correct" (or at least expected) order, call fflush before calling system, to explicitly flush stdout.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: