【发布时间】:2016-10-18 10:49:47
【问题描述】:
"printf" 返回实际打印的字符数,所以我有:
#include<stdio.h>
int main()
{
printf("1");
printf("55555");
printf("10________");
printf("13___________");
printf("18________________");
printf("28__________________________");
}
这个程序会输出
15555510________13___________18________________28__________________________
然后我尝试在gdb中调试,查看gdb的返回值:
(gdb) b main
Breakpoint 1 at 0x804844c: file testp.c, line 4.
(gdb) r
Starting program: /home/a/cpp/a.out
Breakpoint 1, main () at testp.c:4
4 printf("1");
(gdb) n # will return "1" to $eax
5 printf("55555");
(gdb) p $eax # I expect it will print "1" here, wrong!
$1 = 49
(gdb) n
6 printf("10________");
(gdb) p $eax # I expect it will print "5" here, right!
$2 = 5
(gdb) n
7 printf("13____________");
(gdb) p $eax # I expect it will print "10" here, right!
$3 = 10
如您所见,当第一个 printf 运行时,$eax 的值与我的预期不同。后来的值似乎是正确的。 为什么是这样?为什么第一个 printf 不向 $eax 返回“1”?我想 c 风格的 ABI 将返回值存储在 $eax 中,对吧?
谢谢
【问题讨论】:
标签: linux gcc gdb return printf