【C语言】【unix c】同名局部变量之间关系

代码:
            #include <stdio.h>
            #include <sys/types.h>
            #include <unistd.h>
            int j;
            void count(void) {
                static int i = 1;
                printf("++i1 = %d\n", ++i);
                printf("i1 address:%p\n", &i);
                return;
            }
            void count1(void) {
                static int i;
                printf("++i2 = %d\n", ++i);
                printf("i2 address:%p\n", &i);
                return;
            }

            int main(void) {
                int i;
                for(i = 0; i < 5; i++) {
                count();
                count1();
                }
                printf("j address is:%p\n", &j);
                getchar();
                return 0;
            }


        命令: tarena@ubuntu:~/day/day26$ ./a.out 
        结果: pid:4237
            ++i1 = 2
            i1 address:0x804a01c
            ++i2 = 1
            i2 address:0x804a028
            ++i1 = 3
            i1 address:0x804a01c
            ++i2 = 2
            i2 address:0x804a028
            ++i1 = 4
            i1 address:0x804a01c
            ++i2 = 3
            i2 address:0x804a028
            ++i1 = 5
            i1 address:0x804a01c
            ++i2 = 4
            i2 address:0x804a028
            ++i1 = 6
            i1 address:0x804a01c
            ++i2 = 5
            i2 address:0x804a028
            j address is:0x804a02c

        测试编译连接后的状态信息:
        命令: tarena@ubuntu:~/day/day26$ nm a.out 
        结果: 08049f28 d _DYNAMIC
            08049ff4 d _GLOBAL_OFFSET_TABLE_
            0804868c R _IO_stdin_used
                 w _Jv_RegisterClasses
            08049f18 d __CTOR_END__
            08049f14 d __CTOR_LIST__
            08049f20 D __DTOR_END__
            08049f1c d __DTOR_LIST__
            08048800 r __FRAME_END__
            08049f24 d __JCR_END__
            08049f24 d __JCR_LIST__
            0804a020 A __bss_start
            0804a014 D __data_start
            08048640 t __do_global_ctors_aux
            080483c0 t __do_global_dtors_aux
            0804a018 D __dso_handle
                 w __gmon_start__
            08048632 T __i686.get_pc_thunk.bx
            08049f14 d __init_array_end
            08049f14 d __init_array_start
            08048630 T __libc_csu_fini
            080485c0 T __libc_csu_init
                 U __libc_start_main@@GLIBC_2.0
            0804a020 A _edata
            0804a030 A _end
            0804866c T _fini
            08048688 R _fp_hw
            080482f8 T _init
            08048390 T _start
            0804a020 b completed.7108
            08048444 T count
            08048485 T count1
            0804a014 W data_start
            0804a024 b dtor_idx.7110
            08048420 t frame_dummy
                 U getchar@@GLIBC_2.0
                 U getpid@@GLIBC_2.0
            0804a01c d i.2461 //在程序中两个函数的变量名是一样的,但是为了区分,在编译后局部变量的后面会有一个编号
            0804a028 b i.2470 // 变量名前面的d表示已初始化的数据段,b是未初始化的局部变量,也在数据段
            0804a02c B j //变量前面大写字母表示全局变量,小写的代表局部变量
            080484c6 T main
                 U printf@@GLIBC_2.0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘星燎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值