我正在阅读 Brian W. Kernighan 和 Dennis M. Ritchie 所著的《C 编程语言》一书。
在 A.8.9 部分,书上说:
Typedef names may be redeclared in an inner scope, but a non-empty set of type specifiers must be given. For example,
extern Blockno;
does not redeclare Blockno, butextern int Blockno;
does.
我感觉我理解了extern Blockno;
但这 extern int Blockno;
是什么意思? ?
当我尝试这样做时,我收到编译错误,但我仍然不明白这本书的含义!
最佳答案
他的意思是 typedef 名称可以重新声明。例如,它可以是变量名,前提是重新声明发生在内部作用域中。
他指出这样的变量声明
extern Blockno;
不正确。变量应该有一个类型说明符,即声明应该指定声明的实体具有哪种类型。早期int类型被用作默认类型。
因此,声明 int 类型的对象是正确的
extern int Blockno;
即该声明在局部作用域中引入了 int 类型的变量 Blockno
。存储类说明符意味着此声明引用已定义的具有外部或内部链接的对象 Blockno
。
考虑以下演示程序
#include <stdio.h>
int Int = 10;
int main(void)
{
typedef int Int;
{
extern int Int;
printf( "Int = %d\n", Int );
}
return 0;
}
它的输出是
Int = 10
block 作用域的声明
extern int Int;
隐藏外部作用域中声明的 typedef 名称 Int
并引用该声明
int Int = 10;
在全局范围内。
关于c - 在 C 中重新声明 typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58878034/