在C 中多次声明功能和变量

Declaring functions and variables multiple times in C++

本文关键字:功能 变量 声明      更新时间:2023-10-16

在C 中,多次声明变量在编译过程中显示出错误。例如:

int x;
int x;

多次声明功能时不会显示任何错误。例如:

int add(int, int);
int add(int, int);

为什么这种区别在C 中?

请注意,int x;不是(仅)声明,而是定义。因此,由于违反了ODR,因此出现了错误,即在一个翻译单元中只允许一个定义。

变量声明可以写为:

// a declaration with an extern storage class specifier and without an initializer
extern int x;
extern int x;

同时, int add(int, int);是(函数的)声明。一个翻译单元中的多个声明很好,odr不违反。