在C 中多次声明功能和变量
Declaring functions and variables multiple times in C++
在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不违反。
相关文章:
- 将 2D 数组传递给大小为变量的功能
- 将非静态成员函数C 的使用无效,而将成员功能作为函数变量
- 功能原型变量范围C
- 避免为每个成员变量重复功能
- 线程安全性和静态变量/成员功能
- 功能和变量从何而来
- 无全局变量或功能参数的递归调用
- 在已经运行的功能C 中更改变量
- 将功能输出分配给变量
- 从成员变量更新类变量或调用类功能是给出运行时错误
- 如何访问C 中的功能范围变量
- 我的朋友功能无法访问私人变量
- 不同的变量和功能起作用
- 在功能中设置对象变量
- 类和本地功能变量内的全局变量
- 我需要在类C 中定义所有私人功能和变量
- 如何在Arduino的功能中提供软件变量
- 是否可以在C++中推迟 const 变量的初始化,例如 Java 的"blank final"功能?
- 无法从类中的其他功能访问同一类的变量
- 操纵功能变量在doxygen中