2

the problem is when i run this code the system function start first and as per my understanding (which is very little), printf function should start first then the system function !

#include<stdio.h>
#include<stdlib.h>

int main()
{
    printf("test1");
    system("echo 'test2'");
}

and it output :

test2
test1

but the output should be like this instead!

test1
test2
Atrox
  • 107
  • 1
  • 6
  • Just write printf("test1\n"); – Vlad from Moscow Jun 14 '21 at 21:31
  • @VladfromMoscow OP wants to know *why*, and not *how*. – enzo Jun 14 '21 at 21:33
  • 2
    Standard output is usually line buffered by default (when going to a terminal), which means it's only guaranteed to be flushed when a newline is encountered. Your `printf` is unusual in that it does not end in a newline, so it just sits in the buffer while `system` runs. If you want to force a flush, just add `fflush(stdout);` immediately after the `printf` (and before the `system`). Then it will do what you were expecting. – Tom Karzes Jun 14 '21 at 21:33
  • 2
    Why no one is mentioning that `system` is spawning a new process, which is finishing and flushing *its* `stdout`, before the main program does? – Eugene Sh. Jun 14 '21 at 21:35
  • @EugeneSh. Possibly all were waiting for you to write that? :) – CiaPan Jun 14 '21 at 21:55
  • Cannot replicate, MSVC and Windows 7 outputs `test1'test2'` all on one line. – Weather Vane Jun 14 '21 at 22:01
  • There are many duplicates of this problem. Most relate to the use of `fork()`. What happens is that `stdout`, the `FILE*` that `printf` writes to, is buffered. This is line buffered by default on UNIX. But you cannot rely on that. It might be fully buffered or not buffered at all. Since your printf had no new-line the output went into the buffer, to be written at program exit. The system() function executes a new program with its own buffer. So you saw its output first. – Zan Lynx Jun 14 '21 at 22:08
  • 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), [In c language. Why does it seem that system() is called prior to printf()?](https://stackoverflow.com/q/67076867/995714), [Why does “printf” not produce any output?](https://stackoverflow.com/q/39180642/995714) – phuclv Jun 15 '21 at 04:07

2 Answers2

1

You have to add a line break (\n) to the printf statement. I can only give you my basic (probably not entirely correct) explanation. printf gives the characters to a buffer for printing to the console. This buffer is then printed per line. If you add the \n the line is printed instantly.

#include<stdio.h>
#include<stdlib.h>

int main()
{
    printf("test1\n");
    system("echo 'test2'");
}

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
JANO
  • 2,995
  • 2
  • 14
  • 29
0

i just find the explination its when we say printf is actually what we call buffered the output that we ask to print to standard out in this case our display doesn't come to the display immediately it goes into a holding pool a buffer and when that buffer gets filled up it'll dump everything out at once it's an efficiency consideration but if we print out a new line that \n it will go ahead and it will flush the buffer out right ,so to fix it we can use :

#include<stdio.h>
#include<stdlib.h>
int main()
{

printf("test1\n");
system("echo 'test2'");
}

or :

#include<stdio.h>
#include<stdlib.h>

int main()
{

printf("test1");
fflush(stdout);
system("echo 'test2'");
}

both do the same job (flush the output buffer).

Atrox
  • 107
  • 1
  • 6