GDB [[Inferior 1(进程2710)退出代码06]]奇怪的输出

时间:2014-01-14 06:17:52

标签: c gcc macros gdb

我试图测量C程序的输出,所以我运行了调试器。

这是程序 - :

#define swap(a, b) temp=a; a=b; b=temp;
#include <stdio.h>

main()
{
        int i, j, temp;
    i = 5;
    j = 10;
    temp = 0;
    if(i > j)
        swap(i, j);
    printf("%d %d %d",  i, j, temp);
}

此计划的输出10 0 0。我看不出它是怎么可能的。

我在GDB第6,7,8,9,10行放了休息。这就是我得到的 - :

(gdb) run
Starting program: /home/pritishc/Documents/a.out 

Breakpoint 1, main () at ProbleminC.c:7

7   i = 5;

(gdb) print i

$1 = 0

(gdb) print j

$2 = 0

(gdb) continue

Continuing.

Breakpoint 3, main () at ProbleminC.c:8

8   j = 10;

(gdb) print i

$3 = 5

(gdb) print j

$4 = 0

(gdb) print temp

$5 = 32767

(gdb) continue

Continuing.

Breakpoint 4, main () at ProbleminC.c:9

9   temp = 0;

(gdb) print i

$6 = 5

(gdb) print j

$7 = 10

(gdb) print temp

$8 = 32767

(gdb) c

Continuing.

Breakpoint 5, main () at ProbleminC.c:10

10  if(i > j)

(gdb) print i

$9 = 5

(gdb) print j

$10 = 10

(gdb) print temp

$11 = 0

(gdb) c

Continuing.

10 0 0[Inferior 1 (process 2710) exited with code 06]

(gdb) print i

No symbol "i" in current context.

(gdb)

这究竟是什么意思?为什么在地球上它给我这样的输出?

1 个答案:

答案 0 :(得分:1)

你已经通过说:

定义了宏
#define swap(a, b) temp=a; a=b; b=temp;

预处理器会将您的if条件转换为:

if(i > j)
    temp=i;
i=j;
j=temp;

将宏更改为:

#define swap(a, b) { temp=a; a=b; b=temp; }

或者甚至可以使用do ... while循环:

#define swap(a, b) do { temp=a; a=b; b=temp; } while (0)

正在观察6的退出代码是printf的返回值。引自man 3 printf

   Upon successful return, these functions return the number of characters
   printed (excluding the null byte used to end output to strings).

main()(而不是int main())中,添加

return 0;