c - GDB [[Inferior 1 (process 2710) exited with code 06]] 奇怪的输出

标签 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)

这到底是什么意思?为什么它会给我这样的输出?

最佳答案

您通过以下语句定义了宏:

#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;

关于c - GDB [[Inferior 1 (process 2710) exited with code 06]] 奇怪的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21107164/

相关文章:

macros - 在 Chicken Scheme 中扩展宏时出错

python - 使用 ctypes 时,C 函数中的 printf() 在 Python 中输出错误结果

c - file.exe 已停止工作

c - 是否可以使 GCC 自动矢量化器输出内在函数而不是汇编?

c++ - 无法使用静态 TLS 加载更多对象

c - 如何创建宏字符串

c - 多线程程序中的输出

c - 在c程序中接收后字符串以/UN结尾?

c++ - 相同代码中的不一致行为

c++ - # 符号作为 strcmp() 参数的含义