-1

I noticed something strange with the following code.

int main()
{
     printf("Test");      // Section 1 do something here....

     while(1)
     {
           ;
     }
}

Section 1 should be executed first, then the program should get stuck in while loop. But the result was that "Test" didn't get printed, but it got stuck in the while loop. I wonder why the code in Section 1 does not get executed?

I ran the code on Ubuntu 14.04 LTS(compiled with the default gcc compiler)

Adds
  • 601
  • 2
  • 12
  • 24
C.Y. Wu
  • 17
  • 4

2 Answers2

2

The stdout stream is buffered, therefore it will only display what's in the buffer after it reaches a newline. Add :

fflush(stdout);

after line :

printf("Test");

See also this link for other alternatives.

Community
  • 1
  • 1
Marievi
  • 4,951
  • 1
  • 16
  • 33
0

This must work:

#include <stdio.h>

int main() {
    printf("Test");
    while(1){}
}

To compile:

 gcc file.c

To execute:

./a.out
Sergio
  • 844
  • 2
  • 9
  • 26