[英]What is the use extra curly brackets in the code? What does it do?
This is a C program in which the code from "int k" and "for" loop are enclosed in the curly brackets.这是一个 C 程序,其中来自“int k”和“for”循环的代码包含在大括号中。 What is the purpose of those curly brackets?
那些大括号的目的是什么?
int main(){
int k;
{
int k;
for (k=0;k<10;k++);
}
}
There are no "unwanted braces" in this code.这段代码中没有“不需要的大括号”。 There is an anonymous block, which is not an error.
有一个匿名块,这不是错误。 In fact, it is allowed by the spec.
事实上,这是规范允许的。
Your variable k
is defined in the main scope, but then shadowed in the anonymous block.您的变量
k
在主作用域中定义,但随后隐藏在匿名块中。
int main() {
int k = 0;
{
int k = 1;
// do more stuff with k
}
// k is still 0 here.
}
When I was programming C, something like 1000 years ago, I would have had stern words for a dev on my team who tried using this trick.大约 1000 年前,当我编写 C 语言时,我会对我团队中尝试使用此技巧的开发人员说严厉的话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.