3

变量的声明和定义有什么区别

 2 years ago
source link: https://www.cxyxiaowu.com/17266.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

变量的声明和定义有什么区别

C++面试题 2月前 0 230

变量的定义为变量分配地址和存储空间, 变量的声明不分配地址。

一个变量可以在多个地方声明, 但是只在一个地方定义。

加入 extern 修饰的是变量的声明, 说明此变量将在文件以外或在文件后面部分定义。

说明: 很多时候一个变量, 只是声明不分配内存空间, 直到具体使用时才初始化, 分
配内存空间, 如外部变量。

int main()
{
extern int A;
//这是个声明而不是定义,声明A是一个已经定义了的外部变量
//注意:声明外部变量时可以把变量类型去掉如:extern A;
dosth(); //执行函数
}
int A; //是定义,定义了A为整型的外部变量

点击下载C++基础面试题


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK