printf not printing on console
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);
Mr T.
Updated on February 22, 2021Comments
-
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 firstprintf
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
andscanf
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 over 11 yearsSide note, there's nothing wrong with a
fflush()
, they can be quite handy -
KevinDTimm over 11 yearsThat is extremely odd behavior, the \n should flush
stdout
-
alk over 11 yearspossible duplicate of Why does printf not flush after the call unless a newline is in the format string?
-
Mr T. over 11 yearsthis is not a duplicate because i can't use the
fflush
orfprintf
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. over 11 yearsim 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
andscanf
). for the same reason i can't use fflush. -
Kitchi over 11 yearsAre 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. over 11 yearsthe 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 over 11 yearsSometimes 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. over 11 yearsCorrect - 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 over 11 yearsLike I said I'm completely unfamiliar with Eclipse. But this thread may be useful for your problem.
-
yehudahs almost 11 yearsI 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. about 10 yearsYes - this would work and is suggested in the link i posted in the correct answer.
-
Hack-R almost 9 yearsStill not resolved. The buffering has to be disabled or otherwise dealt with in your code.
-
Chris Watts over 8 yearsThis 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 over 6 yearsYou 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 about 4 yearsHas anyone tested this?