首页 > 编程知识 正文

定义与声明,外部变量的定义和声明

时间:2023-05-03 14:31:56 阅读:168467 作者:1710

《C语言深度剖析》及网上许多资料(http://hi.Baidu.com/king cham/item/8f 52370 c 91d c 4920 a 0312 DC7) http://ww.cn blogs.com/wangliang

以下哪个是声明,哪个是定义?

(a ) int i; (b )外部输入I;

来自《C语言深度剖析》的答案是,a是定义,b是声明。

我的回答是,a是声明,b也是声明。

毫无疑问,b是一项声明。 我个人认为a也是声明。 以下是我的理由。

1、根据C语言创始人编写的书《The C programming language second Edition》 A.8,声明(declaration )用于描述每个标识符的含义,不需要为每个标识符保留存储空间。 保留存储空间的声明称为定义(definition )。

根据最后一句话,可以确认定义是声明的一种。

for(intI=0; i9; I )

{

以上故事请读10遍;

}

2、接下来看代码:

#includestdio.hvoid main () { int a; }

上面的int a; 在ARMv8交叉工具链中编译和反汇编的代码如下:

a arch 64-APM-Linux-GNU-gcc-g-o0int.c-oint

a arch 64-APM-Linux-GNU-objdump-alds int.txt

0000000000400560主: (:/home/zql/learnc/int.c :5 # include stdio.hvoidmain ) { int a; } 400560: d65f03c0 ret .

对不起,inta; 是否分配了存储空间? 如果没有,为什么还说inta; 是定义吗?

使用X86下编译器编译和反汇编的代码如下:

0000000000400448主:主(:/home/zql/learnc/int.c :3 # include stdio.hvoidmain ) 40048336055 put

对不起,inta; 是否分配了存储空间? 如果没有,为什么还说inta; 是定义吗?

3、再看一次int a=1; 时:

#includestdio.hvoid main () { int a=1; }

在编译和反汇编armv8工具链之后:

0000000000400560主: (:/home/zql/learnc/int.c :3 # include stdio.hvoidmain ) 4005603360d1000360

sub sp, sp, #0x10/home/zql/LearnC/int.c:4 int a=1; 400564: 52800020 mov w0, #0x1 // #1 400568: b90003e0 str w0, [sp] /home/zql/LearnC/int.c:5} 40056c: 910043ff add sp, sp, #0x10 400570: d65f03c0 ret ...
以下是x86版本:

0000000000400448 <main>:main():/home/zql/LearnC/int.c:3#include<stdio.h>void main(){ 400448: 55 push %rbp 400449: 48 89 e5 mov %rsp,%rbp/home/zql/LearnC/int.c:4 int a=1; 40044c: c7 45 fc 01 00 00 00 movl $0x1,0xfffffffffffffffc(%rbp)/home/zql/LearnC/int.c:5} 400453: c9 leaveq 400454: c3 retq 400455: 90 nop 400456: 90 nop 400457: 90 nop 400458: 90 nop 400459: 90 nop 40045a: 90 nop 40045b: 90 nop 40045c: 90 nop 40045d: 90 nop 40045e: 90 nop 40045f: 90 nop
我对x86汇编不很熟悉,所以拿ARMv8汇编举例说明,但这不会影响C语言的定义。


4、C语言创始人写的书应该是权威吧?请不要拿wxdgq的书来说事。

《The C programming language second Edition》A 8.6有这么一段话:

As other examples, the declarations (看一下其他声明的例子)
 int i, *pi, *const cpi = &i;
 const int ci = 3, *pci;
declare an integer i and a pointer to an integer pi. The value of the constant pointer cpi may not be changed; it will always point to the same location, although the value to which it refers may be altered. 
The integer ci is constant, and may not be changed (though it may be initialized, as here.) The type of pci is ``pointer to const int,'' and pci itself may be changed to point to another place, but the value to which it points may not be altered by assigning through pci. 

上面说,看一下其他声明的例子,其中包括const int ci=3,


以上几个例子说明:

1、int a;没有分配存储空间

2、int a=1;分配了存储空间

所以int a;仅仅是声明,而inta=1;不仅仅是声明,也是定义。


欢迎有不同意见的同学留言讨论。


参考资料:

1、The C programming language second Edition (http://103.2.211.229/videoplayer/The_C_Programming_Language.pdf?ich_u_r_i=dbc101a0839c0caed679934172e59845&ich_s_t_a_r_t=0&ich_e_n_d=0&ich_k_e_y=1345108907751363522410&ich_t_y_p_e=1&ich_d_i_s_k_i_d=4&ich_u_n_i_t=1)

2、《C语言深度剖析》wydbl

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。