问题描述
忽略我为什么要这样做,只是想了解这里发生了什么:此代码编译:
#include typedef 字符字节;int main (void){字节 var_byte;整数字节 = 10;printf("\n 测试程序:%d\n", byte);}
但是,如果我更改声明变量的顺序,它不会编译.
这不会编译:
#include typedef 字符字节;int main (void){整数字节 = 10;字节 var_byte;printf("\n 测试程序:%d\n", byte);}
编译器错误:
b.c:7:8: 错误:在‘var_byte’之前预期‘;’字节 var_byte;^~~~~~~~
有人可以解释为什么订单很重要吗?
在这个程序中
#include typedef 字符字节;int main (void){整数字节 = 10;字节 var_byte;printf("\n 测试程序:%d\n", byte);}
变量名byte
隐藏了typedef的名字.
来自 C 标准(6.2.1 标识符的范围)
- ... 如果标识符在同一名称空间中指定两个不同的实体,则范围可能会重叠.如果是这样,一个实体的范围(内部范围)将严格在另一个范围之前结束实体(外部作用域).在内部作用域内,标识符指定在内部作用域中声明的实体;声明的实体在外部作用域中隐藏(并且不可见)在内部作用域中范围.
注意标识符的名称和 typedef 名称属于同一个命名空间.
typedef 名称在全局作用域(文件作用域)中声明,而变量名在内部块作用域中声明,变量名隐藏了在全局作用域中声明的名称.
考虑这个计划.
#include typedef 字符字节;无效f(无效);int main (void){整数字节 = 10;printf("\n 测试程序:%d\n", byte);F();}无效 f( 无效 ){字节 c = 'A';printf( "%c\n", c );}
在函数 main
的块作用域内(相对于文件作用域的内部作用域),typedef 的名称被同名变量的声明隐藏起来.
但是在函数 f
的块范围内,typedef 中声明的名称是可见的,因为函数块范围内的其他声明都没有隐藏 typedef 中声明的名称.
这是一个更有趣的程序,它处理声明点(它是一个 C++ 术语)
#include size_t 字节 = 255;int main(void){typedef int byte[byte];{字节字节;printf( "sizeof( byte ) = %zu\n", sizeof( byte ) );}返回0;}
它的输出可能看起来像
sizeof( 字节 ) = 1020
这是在文件范围内声明的变量,名称为 byte
size_t 字节 = 255;
在函数main
的外部块作用域中,引入了typedef名称byte
.
typedef int byte[byte];
在声明符声明之后隐藏先前声明的名称byte
.就是在这个 typedef
typedef int byte[byte];
方括号中的名称byte
对应全局名称byte
.
然后在内部块作用域中声明了一个具有相同名称 byte
的数组,该数组隐藏了 typedef 名称.
byte 字节;
注意表达方式
sizeof( 字节 )
使用的是数组的名称而不是 typedef 名称.
Ignoring why I would want to do this, just trying to understand what is happening here:This code compiles:
#include <stdio.h>
typedef char byte;
int main (void)
{
byte var_byte;
int byte = 10;
printf("\n Test program: %d\n", byte);
}
But, if I change the order in which the variables are declared, it does not compile.
This DOES NOT COMPILE:
#include <stdio.h>
typedef char byte;
int main (void)
{
int byte = 10;
byte var_byte;
printf("\n Test program: %d\n", byte);
}
Compiler error:
b.c:7:8: error: expected ‘;’ before ‘var_byte’
byte var_byte;
^~~~~~~~
Could someone please explain why order matters?
In this program
#include <stdio.h>
typedef char byte;
int main (void)
{
int byte = 10;
byte var_byte;
printf("\n Test program: %d\n", byte);
}
the name of the variable byte
hides the name of the typedef.
From the C Standard (6.2.1 Scopes of identifiers)
Pay attention to that the name of an identifier and a typedef name belong to the same name space.
The typedef name is declared in the global scope (file scope) while the name of the variable is declared in the inner block scope and the name of the variable hides the name declared in the global scope.
Consider this program.
#include <stdio.h>
typedef char byte;
void f( void );
int main (void)
{
int byte = 10;
printf("\n Test program: %d\n", byte);
f();
}
void f( void )
{
byte c = 'A';
printf( "%c\n", c );
}
Within the block scope of the function main
(the inner scope relative to the file scope) the name of the typedef is hidden by the declaration of the variable with the same name.
However inside the block scope of the function f
the name declared in the typedef is visible because neither other declaration in the block scope of the function hides the name declared in the typedef.
Here is a more interesting program that deals with the point of declaration (it is a C++ term)
#include <stdio.h>
size_t byte = 255;
int main(void)
{
typedef int byte[byte];
{
byte byte;
printf( "sizeof( byte ) = %zu\n", sizeof( byte ) );
}
return 0;
}
Its output might look like
sizeof( byte ) = 1020
Here is in the file scope there is declared variable with the name byte
size_t byte = 255;
In the outer block scope of the function main
there is introduced typedef name byte
.
typedef int byte[byte];
It hides the previously declared name byte
after declaration of the declarator. That is in this typedef
typedef int byte[byte];
the name byte
in the square braces corresponds to the global name byte
.
Then in the inner block scope there is declared an array with the same name byte
that hides the typedef name.
byte byte;
Pay attention to that in the expression
sizeof( byte )
there is used the name of the array not the typedef name.
这篇关于typedef 和变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!