5

I am trying to make a small program that includes an infinite loop to wait for signal input from the user. I wanted to print out a message about the current working directory before beginning the infinite loop. The message works on its own, but when I put the infinite loop into the code the message does not print out (but the terminal does loop infinitely). The code is:

#include <stdio.h>

int MAX_PATH_LENGTH = 100;

main () {
  char path[MAX_PATH_LENGTH];
  getcwd(path, MAX_PATH_LENGTH);
  printf("%s> ", path);
  while(1) { }
}

If I take out while(1) { } I get the output:

ad@ubuntu:~/Documents$ ./a.out
/home/ad/Documents>

Why is this? Thank you!

A D
  • 729
  • 1
  • 12
  • 28
  • Related: http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-string – itsmatt Sep 22 '11 at 01:45
  • 1
    Others have answered your question, but it should also be noted that an infinite spin loop like this is not a correct programming technique for waiting. It will bog down the system, spin up your fans to max, waste all your battery life, and might even make daemons fly out of your nose. (The upcoming C1x standard will probably allow the compiler to move non-IO code across infinite loops, which could lead to really unexpected stuff...) – R.. GitHub STOP HELPING ICE Sep 22 '11 at 02:00
  • @R.. Thanks for the advice. Is there another technique that you could recommend for me? – A D Sep 22 '11 at 02:10
  • Well you probably want to read input from the user, so you should be calling an input function like `fgets`... – R.. GitHub STOP HELPING ICE Sep 22 '11 at 02:22

5 Answers5

7

When you call printf, the output doesn't get printed immediately; instead, it goes into a buffer somewhere behind the scenes. In order to actually get it to show up on the screen, you have to call fflush or something equivalent to flush the stream. This is done automatically for you whenever you print a newline character* and when the program terminates; it's that second case that causes the string to show up when you remove the infinite loop. But with the loop there, the program never ends, so the output never gets flushed to the screen, and you don't see anything.


*As I just discovered from reading the question itsmatt linked in a comment, the flush-on-newline only happens when the program is printing to a terminal, and not necessarily when it's printing to a file.

David Z
  • 116,302
  • 26
  • 230
  • 268
  • +1 for a complete explanation of both flushing and this particular program. – flight Sep 22 '11 at 01:50
  • Thank you for putting your answer into the context of my particular program and thanks for the extra information! – A D Sep 22 '11 at 01:56
2

Because you don't have a new-line character at the end of your string. stdout is line-buffered by default, which means it won't flush to console until it encounters a new-line character ('\n'), or until you explicitly flush it with fflush().

Oliver Charlesworth
  • 252,669
  • 29
  • 530
  • 650
  • Yes, that's it. If it's desired not to have the newline, fflush(stdout) will flush the buffered data that was waiting for a newline, without one. It may also be possible to change the console mode. – Chris Stratton Sep 22 '11 at 01:48
  • Thank you for the information. fflush() is exactly what I needed, because I wanted to avoid the new-line before the user gave their input. – A D Sep 22 '11 at 01:48
1

Because the stdout hasn't been flushed.

Call

fflush(stdout);

before your loop.

Daniel A. White
  • 174,715
  • 42
  • 343
  • 413
1

Perhaps the output is not getting flushed. Try:

printf("%s> ", path);
fflush(stdout);
flight
  • 6,823
  • 4
  • 22
  • 30
1

Because the output is not flushed. Add

fflush(stdout); 

before the while loop will solve the problem.

Egor
  • 37,239
  • 10
  • 109
  • 127
John Yang
  • 1,210
  • 9
  • 22