40

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?

Toby Speight
  • 23,550
  • 47
  • 57
  • 84
Mr T.
  • 3,810
  • 6
  • 32
  • 57
  • 3
    Side note, there's nothing wrong with a `fflush()`, they can be quite handy – Mike Oct 23 '12 at 16:29
  • 1
    That is extremely odd behavior, the \n should flush `stdout` – KevinDTimm Oct 23 '12 at 16:38
  • 1
    possible duplicate of [Why does printf not flush after the call unless a newline is in the format string?](http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) – alk Oct 23 '12 at 16:48
  • 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. Oct 23 '12 at 19:01

7 Answers7

31

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.

peterh
  • 9,698
  • 15
  • 68
  • 87
kliteyn
  • 1,787
  • 10
  • 23
  • 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=... – yehudahs Jun 30 '13 at 20:37
  • 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. – Chris Watts Aug 26 '15 at 16:09
  • 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. – another Nov 11 '17 at 19:28
22

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.

nbro
  • 12,226
  • 19
  • 85
  • 163
Mr T.
  • 3,810
  • 6
  • 32
  • 57
  • 1
    Still not resolved. The buffering has to be disabled or otherwise dealt with in your code. – Hack-R Jun 01 '15 at 01:32
8

Try setting this before you print:

setvbuf (stdout, NULL, _IONBF, 0);
user1500049
  • 943
  • 7
  • 14
7

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.

Community
  • 1
  • 1
Kitchi
  • 1,724
  • 4
  • 25
  • 46
  • 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. – Mr T. Oct 23 '12 at 16:42
  • Are you outputting to a console of some kind? I work on Linux... so I'm completely unfamiliar with the Windows compilation procedure, etc. – Kitchi Oct 23 '12 at 16:49
  • 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? – Mr T. Oct 23 '12 at 20:07
  • 2
    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. – Kitchi Oct 24 '12 at 10:08
  • 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? – Mr T. Oct 24 '12 at 12:16
  • Like I said I'm completely unfamiliar with Eclipse. But [this](http://wiki.eclipse.org/FAQ_How_do_I_run_Eclipse%3F) thread may be useful for your problem. – Kitchi Oct 24 '12 at 16:29
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);
Tyler
  • 27,580
  • 11
  • 83
  • 103
1

Add c:\gygwin\bin to PATH environment variable either as a system environment variable or in your eclipse project (properties-> run/debug-> edit)

cmd
  • 10,795
  • 6
  • 47
  • 59
1
  1. In your project folder, create a “.gdbinit” text file. It will contain your gdb debugger configuration
  2. Edit “.gdbinit”, and add the line (without the quotes) : “set new-console on”
  3. After building the project right click on the project Debug > “Debug Configurations”, as shown below Debug configuration

  4. In the “debugger” tab, ensure the “GDB command file” now points to your “.gdbinit” file. Else, input the path to your “.gdbinit” configuration file : Gdb configuration

  5. Click “Apply” and “Debug”. A native DOS command line should be launched as shown below Console

Spikatrix
  • 19,378
  • 7
  • 34
  • 77
babyinEclipse
  • 485
  • 10
  • 21