2

Please see the code.

#include "stdio.h"
#include "stdlib.h"

int main() {
    printf("Hello World");
    system("echo \"Hello c\"");
}

operation result:

Hello c
Hello World

Why does it seem that system() is called prior to printf()?

The above test results are running on the manjaro system, but this phenomenon does not exist on the windows system?

Lundin
  • 195,001
  • 40
  • 254
  • 396
aszswaz
  • 609
  • 3
  • 10
  • 4
    Probably buffering, You could try a `fflush(stdout);` call after the `printf()`. – Fred Larson Apr 13 '21 at 14:33
  • 2
    Add `\n` to your `printf` and see if it is changing. This is because the output of `printf` is probably line-buffered and flushed when the program is terminated. And `system` is spawning a separate process at all, which is terminating before the main program. – Eugene Sh. Apr 13 '21 at 14:33
  • duplicate: [Results of `printf()` and `system()` are in the wrong order when output is redirected to a file](https://stackoverflow.com/q/52534629/995714), [`system()` executes before `printf()` even when printf come first](https://stackoverflow.com/q/55301280/995714), [Why does “printf” not produce any output?](https://stackoverflow.com/q/39180642/995714), [Why does printf not flush after the call unless a newline is in the format string?](https://stackoverflow.com/q/1716296/995714) – phuclv Jun 15 '21 at 04:08

3 Answers3

3

This is due to buffering. It looks like to me that echo flushes its buffer when it exits. That output goes to STDOUT. Meanwhile your own Code has the printf output in the buffer and hasn't been flushed yet.

Adding a \n in the end of the printf line would let you print since printf is by default line buffered.

For more information, check out the libc section on buffering here https://www.gnu.org/software/libc/manual/html_node/Stream-Buffering.html

GH0S1
  • 56
  • 2
2

Because

  • you use printf to put something into output buffer
  • you execute a system command with output
  • you implicitly terminate the program, which flushes the output buffer to output
  • the last one above getting the printed output after the system output

You only asked about why, but in case you also want to know about how to change to what you probably expect use the proposals from comments:

  • Fred Larson: flush explicitly
  • Eugene Sh. : flush implcitily with a newline in output
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
0

You have your output line in the stdout buffer, because you have not ended it in a '\n'. If you end your output line with it, you'll se the line printed before the call to system.

The reason is that stdio uses line buffer if the output device is a terminal. It buffers all the characters until the buffer is full (this is difficult to see, as a full buffer can be as big as 16-32kb, it varies from system to system) or a new line is output. Then the buffer is flushed. But you didn't use a '\n', so the buffer is waiting until the program ends after the call to system(3).

You have several ways to solve this:

  • You can force the buffer flush by calling fflush(stdout);. This will make stdio to write the buffer as it is, and you will see your line followed (without a new line) by the output of your system() command.
  • You can tell stdio not to use a buffer on stdout. This is done with setbuf(stdout, NULL);. In this way, every printf() you make will be written as the strings are being formatted, as there's no buffer to hold them off.
  • You can end your output in a '\n'. You'll see how different the output is by just putting a new line before the call to system().

In case your output is not to a tty device, but to a file, then the situation is worse, as then the buffer flush is done only when the buffer fills completely (not on line endings) and you'll see the full output of your system() call before all the output of your program (assuming you print several lines before calling printf() and both, the called program and the output of this one are redirected to a file)

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31