- 论坛徽章:
- 0
|
10可用积分
我写了一个小程序,一个函数f没有在main里面被调用,而是希望调试的时候去打印一些信息,"C专家编程"里面把这个叫做debugging hook。但是下面这个小程序却没有像我期望的那样工作。这是为什么呢?
----------------------------
#include<stdio.h>
#include<stdlib.h>
int i=11;
void f(){
printf("i=%d\n");
}
int main(void){
i=2;
i=i*5;
i=i+6;
return 0;
}
----------------------------
然后我编译并调试
kg@opensolaris:~$ gcc -g t.c
kg@opensolaris:~$ gdb a.out
GNU gdb 6.3.50_2004-11-23-cvs
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-pc-solaris2.11"...
(gdb) list
1 #include<stdio.h>
2 #include<stdlib.h>
3 int i=11;
4 void f(){
5 printf("i=%d\n");
6 }
7 int main(void){
8 i=2;
9 i=i*5;
10 i=i+6;
(gdb) b 8
Breakpoint 1 at 0x8050cb0: file t.c, line 8.
(gdb) r
Starting program: /export/home/kg/a.out
Breakpoint 1, main () at t.c:8
8 i=2;
(gdb) call f()
i=0------------------------->为什么是 0??????
(gdb) n
9 i=i*5;
(gdb) call f()
i=0------------------------->为什么是 0??????
(gdb) n
10 i=i+6;
(gdb) call f()
i=0------------------------->为什么是 0??????
问题: 为什么f()不能打印出全局变量i的值呢? 谢谢啦! |
最佳答案
查看完整内容
你的程序写错了吧?printf("i = %d\n",i);
|