2

While trying to debug some C code I noticed that a printf() won't execute if placed before an infinite loop. Does anyone know why this is? Practically it's not that big of a deal, but for debugging it's a nightmare.

#include<stdio.h>

int main()
{
  int data;

  printf("This prints fine.\n");  

  printf("Enter data: ");
  scanf("%d", &data);

  printf("This should print but it doesn't.\n");

  while(1)
  {
    //Infinite Loop
  }

  return 0;
}
Keith Thompson
  • 242,098
  • 41
  • 402
  • 602
amishjack
  • 21
  • 5
  • Yeah that works. What exactly is going on here? – amishjack Dec 02 '12 at 08:25
  • 3
    I prefer to print debug info to stderr, which typically isn't buffered. – Vaughn Cato Dec 02 '12 at 08:29
  • 1
    What platform? Compiler version? Is this the actual code? If so, why is your `include` missing the `#`? Do you really have that `\n` at the end of that last string? – AnT Dec 02 '12 at 08:30
  • @user1869992:- Please read my answer for explanation!!! – Rahul Tripathi Dec 02 '12 at 08:31
  • 5
    Having `\n` at the end of the string is typically sufficient to ensure that the string appears on the screen. `fflush` should not be necessary. I'd guess that the code above is inaccurate and the last `printf` does not really have the `\n` at the end. – AnT Dec 02 '12 at 08:32
  • @AndreyT actually it's not if `stdout` is redirected, check my answer – iabdalkader Dec 02 '12 at 08:39
  • This is the actual code. Even with the `\n` the second printf will not execute. See @RahulTripathi's answer for an explanation. – amishjack Dec 02 '12 at 08:40
  • @amishjack: The answer looks like an attempt to tailor the most simple explanation to the observed behavior, instead of trying to find the real reason. A *typical* standard output is *line buffered*, which means that it is flushed automatically when a new-line character is encountered. If your standard output is different (could be), then it would be interesting to know what platform you are using. – AnT Dec 02 '12 at 08:44
  • Just for you @AndreyT i686-apple-darwin11-llvm-gcc-4.2 – amishjack Dec 02 '12 at 08:49
  • @amishjack are you redirecting the output or printing to screen ? – iabdalkader Dec 02 '12 at 08:58
  • @mux Printing to the screen. – amishjack Dec 02 '12 at 09:03
  • @AndreyT you were right, it doesn't explain it, I removed my answer. – iabdalkader Dec 02 '12 at 09:09

3 Answers3

2

On calling printf() , output is displayed after program terminates or newline character is encountered. But since you are calling infinite loop after printf() , program doesn't terminate and output from buffer is not displayed.

Use fflush(stdout) to force output from buffer to be displayed

stdout The standard output stream is the default destination of output for applications. In most systems, it is usually directed by default to the text console (generally, on the screen).

The fflush() function causes the system to empty the buffer

Mudassir Hasan
  • 26,910
  • 19
  • 95
  • 126
0

try __fpurge(stdout) It will clear your output buffer

suren
  • 1,574
  • 2
  • 11
  • 7
0

Their is a concept called line buffer and block buffer. Every thing what you want to write to the screen will be in buffer. line buffer means when ever new line [\n] found it"ll flush the buffer that means your string will print on the screen. block buffer means their will be fixed size for block until unless that block full it"ll not print on the screen. in the above case new line [\n] is enough to print

Shashank
  • 34
  • 4