6

I am trying to understand system calls made in c++ using system("some command"). here's the code

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
  cout << "Hello ";
  system("./pause");
  cout << "World";
  cout << endl;

  return 0;
}

the executable "pause" is created from the following code

#include <iostream>
using namespace std;

int main()
{
    cout<<"enter any key to continue\n";
    cin.get();
    return 0;

}

I get the following output

enter any key to continue
1
Hello World

Can someone please explain the output to me? I was expecting this -

Hello
enter any key to continue
1
World
Petr
  • 9,812
  • 1
  • 28
  • 52
  • 1
    Your expectation is because of `std::cout` buffering, it is not related to system. If you change `cout << "Hello ";` to `cout << "Hello" << endl;` you should see what you expected. – Telokis Jul 28 '15 at 11:20
  • 3
    Note that the name "system call" is already taken by [another concept](https://en.wikipedia.org/wiki/System_call). – nwp Jul 28 '15 at 11:35

4 Answers4

6

It's probably not a case of system call but output stream buffering.

cout << "xxx" does not necessary outputs something, so program called by system can be executed before cout flushes it buffer to console.

try adding cout.flush() after cout << "Hello" or write cout << "Hello" << flush

also: cout << endl automagically calls flush

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Hcorg
  • 11,598
  • 3
  • 31
  • 36
  • I wouldn't call it "automagically" since it's very explicitly defined to call `flush` – Jonathan Wakely Jul 28 '15 at 15:39
  • @JonathanWakely for person who start working with C++ streams it seems as little magic, as many people think, that `std::endl` is a `std::string` with `"\n"` inside. 'automagic' was referring to 'calling function without () visible'. Maybe not best words :) – Hcorg Jul 28 '15 at 15:54
  • Ah ok, then I guess it does seem like magic from that perspective :) – Jonathan Wakely Jul 28 '15 at 16:28
6

system runs a command in a shell. But your problem is not with system but with cout. cout is line-buffered, ie. it won't flush (write out) its data until a new line character is encountered. You need to flush it explicitely with cout << flush.

StenSoft
  • 9,369
  • 25
  • 30
5

The reason for the particular behavior that you observe seems to be just cout buffering: the Hello is not printed immediately, instead held in a buffer until endl is output (or the buffer is filled completely, or you explicitly call flush()). This is in no way related to a system() call.

A simpler example:

cout << "Hello";
sleep(10);
cout << "World";

Both words will appear at the same time, not with a 10 seconds delay.

Petr
  • 9,812
  • 1
  • 28
  • 52
4

The answer on "how does system library function works?" is usually operating system specific. See here for a Linux point of view. Notice that system is not a system call and there is a priori no relation between using system(3) and having cout buffer being flushed.

You should flush the stdout before calling system

 cout << "Hello " << flush;

or preferably

 cout << "Hello " << endl;

The behavior you are observing is because cout is buffered and you forgot to flush the buffer.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • my question has more to do with the sequence of execution of code. "hello" should have been printed first, followed by the system call and then world – aseembits93 Jul 28 '15 at 11:20