-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
  • 463
  • 2
  • 8
  • 22
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,761
  • 1
  • 10
  • 31
0

This must work:

#include <stdio.h>

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

To compile:

 gcc file.c

To execute:

./a.out
Sergio
  • 744
  • 1
  • 8
  • 24