在我的 .h 文件中,我有
extern int a[4];
在我的 .c 文件中
int a[10];
那么这有什么问题吗?
声明和定义的大小很重要吗?不对吗?
如果我在其中一个文件中写入 sizeof(a)
,输出将会是什么?
这是未定义的行为吗?
最佳答案
如果您在源文件中包含头文件,则 a
的两个声明必须具有与 C 相同的类型:
(C11, 6.7p4) "All declarations in the same scope that refer to the same object or function shall specify compatible types."
即使两个声明位于两个翻译单元中,它们也需要具有相同类型:
(C11, 6.2.7p2) "All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined."
关于c - 定义和声明应该匹配吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471324/