The output in VS code doesn't appear in terminal - c

i have used debugger and try to debug simple program but no output appear in terminal
photo of the program:

If program was well compiled - then you are a victim of buffered stdout stream, that is used by default with printf function.
Changing call to add \n at the end will solve the problem for you. printf("your tex\n").
See this SO post to understand background: Why does printf not flush after the call unless a newline is in the format string?

Related

using STDOUT from within gdb

I have a function that pretty prints a data structure, its function prototype is:
void print_mode(FILE *fp, Mode *mode);
the FILE* allows you to redirect the output to anywhere you want, e.g. stdout, stderr, a file etc. Mode is the data structure
I am trying to call this function from within gdb and want the output to be directed to the gdb console window, stdout?
I have tried:
(gdb) p print_mode(STDOUT,fragment_mode)
No symbol "STDOUT" in current context.
(gdb) p print_mode(stdout,fragment_mode)
$17 = void
neither of which work
any ideas how i can get the output of the function to display in the gdb console?
should add - I am using gdb within emacs 24.2.1 under linux
STDOUT seems to be macro, which is not know to GDB, as handled prior to compilation by the pre-preprocessor.
Using stdout should do the job.
However the function print_mode() simply does not seem to print out anything.
In terms what's being printed to the console by the program being debugged, GDB's commands printand call should not make a difference.
For details on this you might like to read here: https://sourceware.org/gdb/onlinedocs/gdb/Calling.html
An issue might be that stdout by default is line buffered, so output would not occur before detecting a linefeed and print_mode() perhaps does not send a linefeed (\n).
To test this just use stderr as output file, as the latter isn't buffered:
p print_mode(stderr, fragment_mode)
Oh dear - silly mistake. You're right, stdout does do the job.
I forgot that having upgraded from emacs 23 to 24, the way gdb works has changed in as much as it now opens a separate buffer *input/output of program-name* to which it redirects the output of the program being debugged. In the prior version of emacs it was all displayed in the same, single gdb buffer.
So my second attempt was actually working, I was just looking in the wrong place so didn't see the output

c code is inexplicably skipping lines

When I compile this small program for some reason the "printf("done")" line is not executing and I cannot figure out why. There was originally an if block before my print statement and that also was not executing. I tried running it through gdb and stepping through it. It found the line but it did not execute it. I am trying to compile it using gcc 4.7.2 and I'm using gedit 3.4.2 as my text editor.
Here is the command I'm using to compile it.
gcc teststuff.c -o test
And here is the code
#include <stdio.h>
void testmethod()
{
int sign = 1;
printf("hello\n");
printf("%d\n",sign);
printf("done");
}
main(void)
{
testmethod();
return 0;
}
Anybody have any ideas why it is skipping that part of the code? Thanks!
Put a line break at the end.
printf("done\n");
Standard output is often line-buffered, that means that the data you write only gets flushed to the screen once you finish a line (unless you explicitly flush it, such as with fflush()). However, it should be flushed when the program exits (but not necessarily before the program exits).
Also, depending on your shell, if a program writes data without a linebreak you might get your command prompt on top of the output:
my-computer ~/projects/my-program $ ./a.out
hello
1
donemy-computer ~/projects/my-program $
This is because your program finishes and ends before the Done line gets the chance to be printed to the console.
Console output is generally buffered. The string "done" stays in the buffer of stdout until the stream is flushed. You can do it manually by calling fflush(stdout), or by adding \n to the end of the "done" string. Printing the end-of-line character causes console stream to empty the buffer onto the screen (unless you set a special output mode that lets \n to be buffered until an explicit flush).

printf not printing on console

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?
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.
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.
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.
Try setting this before you print:
setvbuf (stdout, NULL, _IONBF, 0);
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);
Add c:\gygwin\bin to PATH environment variable either as a system environment variable or in your eclipse project (properties-> run/debug-> edit)
In your project folder, create a “.gdbinit” text file. It will contain your gdb debugger configuration
Edit “.gdbinit”, and add the line (without the quotes) : “set new-console on”
After building the project right click on the project Debug > “Debug Configurations”, as shown below
In the “debugger” tab, ensure the “GDB command file” now points to your “.gdbinit” file. Else, input the path to your “.gdbinit” configuration file :
Click “Apply” and “Debug”. A native DOS command line should be launched as shown below

eclipse cygwin printf no output on debug

I managed to get cygwin to work with eclipse and made the default hello world program. Running the program is fine and it outputs correctly in console. But when I debug and step through the program, printf statements does not appear in console. Is there a setting somewhere that I need to change?
You should add this once or after every printf
fflush(stdout);
For reasons I don't really know, the program output will occur only at the end of the program. Putting this call will force the output everytime there is a printf

C program output in wrong order Eclipse

I have set up Eclipse for c programming on my Windows machine, I have successfully run a "hello, world" program. However, when I try to ask for user input and run the program the console on Eclipse is displaying in the wrong order.
Here is what I have
#include <stdio.h>
int main(void){
char letter;
printf("Please enter a letter:\n");
scanf(" %c, &letter);
printf("The letter you have selected is: %c", letter);
return 0;
}
This program builds just fine, and it runs just fine outside of Eclipse. But when I run it in Eclipse I get the output:
E <--- (this is my user input)
Please enter a letter:
The letter you have selected is: E
I'm not sure why the output is executing in the wrong order, so any help would be much appreciated! Thank you.
Yeah, Eclipse will buffer a certain amount of output (I don't remember how much off hand) before it will appear in the output window. Eclipse is communicating with the attached process through a pipe which is fully buffered. It won't flush until either fflush() is called or the buffer is full. I found that when debugging with Eclipse, things work best if I put the following near the beginning of my application:
setvbuf(stdout, NULL, _IONBF, 0);
This will cause stdout to flush immediately whenever it is written to. If you want to use that for debugging and turn it off otherwise, you can conditionally compile it:
#ifdef DEBUG
setvbuf(stdout, NULL, _IONBF, 0);
#endif
No need to put fflush() everywhere this way.
Edit
Here's where I found the solution when I first ran into this issue myself.
http://wiki.eclipse.org/CDT/User/FAQ#Eclipse_console_does_not_show_output_on_Windows
Eclipse's console is not a true console or terminal but rather eclipse is communicating with the attached process through a pipe which is fully buffered not line buffered. This is why a newline '\n' does not cause the buffer to be flushed.
It sounds like Eclipse is buffering the output of your program and not displaying it right away. This indicates that the "run within Eclipse" feature is not intended to run interactive programs.
You could try adding fflush(stdout); after the first printf, but you shouldn't have to do that just to make your program work in a particular environment.
Try adding fflush(stdout); after the first printf. This has a decent chance of being of help, in case Eclipse does not auto-flush after '\n'.
Yes, fflush()ing buffers is necessary to keep the console's screen updated ...
... but please guys, it's not Eclipse's fault in- and output might get out of sync, but the library's in use!

Resources