printf not printing on console

210,035

Solution 1

Apparently this is a known bug of Eclipse. This bug is resolved with the resolution of WONT-FIX. I have no idea why though. here is the link: Eclipse C Console Bug.

Solution 2

Output is buffered.

stdout is line-buffered by default, which means that '\n' is supposed to flush the buffer. Why is it not happening in your case? I don't know. I need more info about your application/environment.

However, you can control buffering with setvbuf():

setvbuf(stdout, NULL, _IOLBF, 0);

This will force stdout to be line-buffered.

setvbuf(stdout, NULL, _IONBF, 0);

This will force stdout to be unbuffered, so you won't need to use fflush(). Note that it will severely affect application performance if you have lots of prints.

Solution 3

You could try writing to stderr, rather than stdout.

fprintf(stderr, "Hello, please enter your age\n");

You should also have a look at this relevant thread.

Solution 4

Try setting this before you print:

setvbuf (stdout, NULL, _IONBF, 0);

Solution 5

As others have pointed out, output can be buffered within your program before a console or shell has a chance to see it.

On unix-like systems, including macs, stdout has line-based buffering by default. This means that your program empties its stdout buffer as soon as it sees a newline.

However, on windows, newlines are no longer special, and full buffering is used. Windows doesn't support line buffering at all; see the msdn page on setvbuf.

So on windows, a good approach is to completely shut off stdout buffering like so:

setvbuf (stdout, NULL, _IONBF, 0);
Share:
210,035
Mr T.
Author by

Mr T.

Updated on February 22, 2021

Comments

  • Mr T.
    Mr T. about 3 years

    I’m getting started in the C language. I am using eclipse (juno) as my IDE and installed CDT plugin. I have also unpacked mingw64 (GCC Compiler). I wrote a very simple program to see if it works. This is my code:

    #include <stdio.h>
    
    int main()
    {
        int age;
        printf("Hello, please enter your age:\n");
        scanf("%d", &age);
        printf("Your age is %d", age);
        return 0;
    }
    

    The problem is that the output buffer is filled with the string value of the first printf but does not output it to the console. I have to enter a number, and only then the buffer pours all the data to the console so I see the console something like this:

    1
    Hello, please enter your age:
    Your age is 1
    

    instead of what is expected that is:

    Hello, please enter your age:
    1
    Your age is 1
    

    Now, I found that I can use fflush(stdout) after the first printf but I don't think that this solution is elegant and even necessary. Any ideas on how I can overcome this?

    EDIT - because I'm learning this in my university, I can't use anything that wasn't learned in the course so I can only use printf and scanf

    NEW EDIT - I think I have found an explanation for this. As I said, I am outputting to the console view inside Eclipse. The strange thing is that if I compile and run the program from the command line of Windows, I get the wanted result. Therefore, I think that eclipse is actually writing the output to a file and presenting it in the console window. How can I force eclipse to open a real command line window in my run configurations?

    • Mike
      Mike over 11 years
      Side note, there's nothing wrong with a fflush(), they can be quite handy
    • KevinDTimm
      KevinDTimm over 11 years
      That is extremely odd behavior, the \n should flush stdout
    • alk
      alk over 11 years
    • Mr T.
      Mr T. over 11 years
      this is not a duplicate because i can't use the fflush or fprintf functions. When i run the gcc from the command line in windows, i get the correct result so i suspect its a case of wrong eclipse configuration. Any ideas which configurations?
  • Mr T.
    Mr T. over 11 years
    im taking this C course in my university and because is a 101 course then i can't use stuff that isn't in the material (i can only use for now printf and scanf). for the same reason i can't use fflush.
  • Kitchi
    Kitchi over 11 years
    Are you outputting to a console of some kind? I work on Linux... so I'm completely unfamiliar with the Windows compilation procedure, etc.
  • Mr T.
    Mr T. over 11 years
    the console that im talking about is the console in the eclipse IDE. (Console view) - but as i said in another comment, when i compile via the command line with gcc, i get the wanted result so i strongly suspect that this is an eclipse configuration issue. any ideas?
  • Kitchi
    Kitchi over 11 years
    Sometimes if the compiler thinks that it's outputting to a file of some kind rather than a console, it won't flush the buffer until it's completely full, even on a newline. Maybe that's what is happening, since you get what you wanted via gcc.
  • Mr T.
    Mr T. over 11 years
    Correct - so is it possible to force eclipse to run the c program in the windows command line instead of the Console view of eclipse?
  • Kitchi
    Kitchi over 11 years
    Like I said I'm completely unfamiliar with Eclipse. But this thread may be useful for your problem.
  • yehudahs
    yehudahs almost 11 years
    I tried this solution and found out that you can't step printf in debug mode. it is stuck and I am getting : *stopped,reason="end-stepping-range",frame=...
  • Mr T.
    Mr T. about 10 years
    Yes - this would work and is suggested in the link i posted in the correct answer.
  • Hack-R
    Hack-R almost 9 years
    Still not resolved. The buffering has to be disabled or otherwise dealt with in your code.
  • Chris Watts
    Chris Watts over 8 years
    This was the solution for me. I could see the output of my program if I ran it directly in a terminal window, but if I tried to use tee or even write the terminal output to a file, I would get nothing.
  • another
    another over 6 years
    You are right. In my case, the output was buffered, and unloaded after the program ended, writing all the buffered strings it has accumulated during the execution, right after ending the program.
  • Danijel
    Danijel about 4 years
    Has anyone tested this?