Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

c - Why does printf output not appear right away when stepping through the code?

I'm using Anjuta and gdb on Fedora 20 and created a C Makefile project. The code looks like this:

#include <stdio.h>

int main (void)
{
°   printf ("1");
°   printf ("2");
°   printf ("3");

    return (0);
}

° means I set a breakpoint at that position.

Now when I debug the code, there's no output while the current line is one of these printf-functions. Only when I exit main '123' appears in the terminal.

If I add to the second printf argument, then '12' appears as output when I move from breakpoint 2 to the 3rd one.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By default, stdout is line buffered when writing to a terminal, fully buffered when writing to any other type of stream. Since you're not printing any newlines, the output is being buffered. You can change the buffering mode with setbuf(), end each string with newline, or call fflush() when you want printing to take plac.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...