我知道,在C#和JavaScript中,以下内容是完全有效的:
{
var foo;
}
一个裸块在C中也有效吗?
也就是说,这是有效的C?
{
int foo;
}
发布于 2015-01-25 14:08:26
这在C中也有效吗?
是的,它是,它被称为复合语句。
来自C11标准:
6.8.2 Compound statement
Syntax
1 compound-statement:
{ block-item-listopt }
block-item-list:
block-item
block-item-list block-item
block-item:
declaration
statement
复合语句本身就是C中的语句。
例如,这个块是一个有效的块:
{
{
{
printf("Hello world");
}
}
}
即使是这个也是有效的:
{{{}}}
{}
是一个空的复合语句。
发布于 2015-01-25 14:09:35
是的,完全没问题!
例如:
#include <stdio.h>
int main() {
{
int i = 5; //If you declare i outside you can use both print statements
printf("%d", i);
}
//printf("%d", i); Note that i is out of scope here
return 0;
}
https://stackoverflow.com/questions/28137287
复制相似问题