2

Why is the output of the printf is not show when stepping out the line? But at some point it did print line 16.

c file:

#include<stdio.h> void nextfunc(){ int ctr; for(ctr = 0; ctr<3; ctr++){ printf("print ctr = %d",ctr); } printf("last print"); } void main(){ int x; printf("input x: "); scanf("%d",&x); printf("\nprint 2"); printf("\nprint 3"); nextfunc(); } 

GDB:

 (gdb) break main Breakpoint 1 at 0x8048479: file file5.c, line 14. (gdb) break nextfunc Breakpoint 2 at 0x804843a: file file5.c, line 6. (gdb) run Starting program: /home/charmae/workspace/AVT/file5 Breakpoint 1, main () at file5.c:14 14 printf("input x: "); (gdb) s 15 scanf("%d",&x); (gdb) s input x: 4 16 printf("\nprint 2"); (gdb) s 17 printf("\nprint 3"); (gdb) s print 2 18 nextfunc(); (gdb) s Breakpoint 2, nextfunc () at file5.c:6 6 for(ctr = 0; ctr<3; ctr++){ (gdb) s 7 printf("print ctr = %d",ctr); (gdb) s 6 for(ctr = 0; ctr<3; ctr++){ (gdb) s 7 printf("print ctr = %d",ctr); (gdb) s 6 for(ctr = 0; ctr<3; ctr++){ (gdb) s 7 printf("print ctr = %d",ctr); (gdb) s 6 for(ctr = 0; ctr<3; ctr++){ (gdb) s 9 printf("last print"); (gdb) s 10 } (gdb) s main () at file5.c:19 19 } (gdb) s 0x0014a113 in __libc_start_main () from /lib/i386-linux-gnu/libc.so.6 (gdb) s Single stepping until exit from function __libc_start_main, which has no line number information. print 3print ctr = 0print ctr = 1print ctr = 2last print[Inferior 1 (process 2578) exited with code 012] 

3 Answers 3

2

Output though stdout is buffered. That means it is saved in a temporary buffer either until the buffer is full, there is a newline being printed or the function fflush(stdout) is called. stdout is flushed automatically also when the program ends.

The reason your output is printed "on the wrong place" in GDB is because of this buffering. you should either add newlines to the end of the printf format string, or explicitly call fflush.

Sign up to request clarification or add additional context in comments.

Comments

2

stdio functions like printf are buffered, so it outputs only when it encounters newline.

If you want immediate print use newline character \n or fflush(stdout); after every printf

edit: Why does printf not flush after the call unless a newline is in the format string?

1 Comment

Thanks for the reference. I find this import, as it mentions, that even standard output is not line buffered (but simply buffered), if it has been redirected.
1

The trick is that you didn't put in a newline:

printf("last print"); 

The standard IO library is going to buffer the output until it sees a newline to print. Output to terminals is usually line-buffered; probably gdb runs the program as if it is connected to a terminal, so it prints the previous lines with the \n character in them as they are printed.

The output actually is in the output you've got:

... ctr = 2last print[In ... 

The standard IO library flushes all its input streams just before exiting -- via an atexit(3) exit handler -- so the output gets flushed just before the program asks the operating system to tear down its memory and inform its parent that it is dead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.