-5
int main()
{
    printf("main started");
    for(;;) {}
    return 0;
}   

The problem is this it does not print "main started". Doesn't matter what is in for loop and how many statements are there before loop.

Scroll-lock
  • 128
  • 1
  • 7
  • 2
    The output is buffered. – lurker Aug 06 '13 at 14:25
  • 1
    Please provide more information. What compiler do you use and what is the platform that you are working on? – Kelm Aug 06 '13 at 14:25
  • 1
    Sure it does matter what is there in the for loop: put `return 0;` in it and see what happens. –  Aug 06 '13 at 14:26
  • hugh, main() is a technically corrrect way to declare the main entry point, but please use 'int main(){ ... return 0;}. – lucasg Aug 06 '13 at 14:26
  • 1
    @georgesl It's neither technically correct in the current version of C nor in any version of C++. So agreed with your conclusion, use `int main()` :) –  Aug 06 '13 at 14:27
  • gcc compiler and ubuntu 12.10 c language – Scroll-lock Aug 06 '13 at 14:33
  • @Scroll-lock, gcc compiles C in C89 mode by default. Which is definitely not the modern version and even not the previous one – sasha.sochka Aug 06 '13 at 14:36
  • In Windows a \n does NOT cause the buffer to flush. Use fflush(stdout); for portability to all platforms. Also, the buffer is flushed when the main program terminates, which it NEVER does in your example. Is `for(;;) {}` necessary?? – JackCColeman Aug 06 '13 at 23:21
  • no ... A semicolon would do it @JackCColeman – Scroll-lock Aug 07 '13 at 16:06
  • @Scroll-lock, what I mean is, is the infinite loop implemented by the for statement necessary? The program never exits and never returns and thus never has a chance to flush the buffer, which is why issuing a `fflush` immediately after `printf` is one solution. – JackCColeman Aug 07 '13 at 16:27

4 Answers4

2

Put a newline at the end of the output. Without it the text goes into the output buffer but doesn't get flushed.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
2

You need to flush the output:

fflush(stdout);

Or, terminate the string by \n character as most systems have line buffered standard output.

mohit
  • 5,696
  • 3
  • 24
  • 37
1

printf prints the string only after flushing it. It auto flushes the data if you add \n (newline) symbol in the string (although it usually works, there is no guarantee! Don't rely on it). Data is also auto-flushed when the program finishes - after returning from the main function or calling exit.

In your case program never finishes because of the for loop without a condition, autoflush is never called as a result

If you want to flush it anyway you can force flush yourself calling fflush(stdout). The same function is called automatically at exit.

See the similar question which I asked earlier about standard guarantees regarding auto-flush. Is there a guarantee of stdout auto-flush before exit? How does it work?

Community
  • 1
  • 1
sasha.sochka
  • 14,395
  • 10
  • 44
  • 68
1

What you print with printf() is saved in a buffer. This means that it's not immediately sent to your console. However, this buffer is flushed:

  • when you print a new-line (\n),
  • if you ask for a flush,
  • or when the buffer becomes too full, and gets flushed automatically.

So, in your case, to see it immediately, add a fflush(stdout), or a do printf("main started\n").

meaning-matters
  • 21,929
  • 10
  • 82
  • 142