gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:50:22 25 4
gpt4 key购买 nike

我试图测量 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com