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]
charmae
  • 1,072
  • 13
  • 38

3 Answers3

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.

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
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?

Community
  • 1
  • 1
Alam
  • 1,318
  • 13
  • 25
  • 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. – alk Nov 23 '11 at 07:48
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.

sarnold
  • 96,852
  • 21
  • 162
  • 219