在 C99 中哪里可以合法地声明变量?
当我第一次接触 CI 时,他们告诉我要始终在函数顶部声明变量。现在我已经掌握了这门语言,我将重点放在编码风格上,特别是限制变量的范围。我已经了解了限制范围的好处,并且遇到了一个有趣的例子。显然,C99 允许你这样做...
for (int i = 0; i < 10; i++)
{
puts("hello");
}
我原以为变量范围受到最内层周围大括号{ }
的限制,但在上面的示例中int i
的范围似乎受到 for 循环的花括号的限制,即使它是在花括号之外声明的。
我尝试使用 fgets()
扩展上面的示例来执行我认为类似的操作,但这两个都给了我一个语法错误。
fgets(char fpath[80], 80, stdin);
*参见注释**
fgets(char* fpath = malloc(80), 80, stdin);
所以,在 C99 中声明变量到底在哪里是合法的? for 循环示例是该规则的例外吗?这是否也适用于 while
和 do while
循环?
*注意**:我什至不确定这在语法上是否正确,即使我可以在那里声明 char 数组,因为 fgets()
正在寻找指针to char 而不是指向 char 数组 80 的指针。这就是我尝试 malloc()
版本的原因。
When I was first introduced to C I was told to always declare my variables at the top of the function. Now that I have a strong grasp of the language I am focusing my efforts on coding style, particularly limiting the scope of my variables. I have read about the benefits to limiting the scope and I came across an interesting example. Apparently, C99 allows you to do this...
for (int i = 0; i < 10; i++)
{
puts("hello");
}
I had thought that a variables scope was limited by the inner-most surrounding curly braces { }
, but in the above example int i
appears to be limited in scope by the curly braces of the for-loop even though it is declared outside of them.
I tried to extend the above example with fgets()
to do what I thought was something similar but both of these gave me a syntax error.
fgets(char fpath[80], 80, stdin);
*See Note**
fgets(char* fpath = malloc(80), 80, stdin);
So, just where exactly is it legal to declare variables in C99? Was the for-loop example an exception to the rule? Does this apply to while
and do while
loops as well?
*Note**: I'm not even sure this would be syntactically correct even if I could declare the char array there since fgets()
is looking for pointer to char not pointer to array 80 of char. This is why I tried the malloc()
version.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C99 中,您可以在需要的地方声明变量,就像 C++ 允许您这样做一样。
您可以在“for”循环的控制部分声明变量:
您不能在“while”循环或“if”语句的控制部分声明变量。
不能在函数调用中声明变量。
显然,您可以(并且始终可以)在任何循环或“if”语句之后在块中声明变量。
C99标准说:
In C99, you can declare your variables where you need them, just like C++ allows you to do that.
You can declare a variable in the control part of a 'for' loop:
You cannot declare a variable in the control part of a 'while' loop or an 'if' statement.
You cannot declare a variable in a function call.
Obviously, you can (and always could) declare variables in the block after any loop or an 'if' statement.
The C99 standard says:
我要注意的第一件事是,您不应该混淆
,
第一个是控制结构,而第二个是函数调用。控制结构以与函数调用非常不同的方式评估其 parens() 内的文本。
第二件事是......我不明白你想说什么:
您为 for 循环列出的代码是 C 中非常常见的结构,并且变量“i”确实应该在 for 循环体内可用。即,以下内容应该有效:
我是否误读了您所说的内容?
The first thing I'd note is that you shouldn't confuse
and
The first is a control structure while the second is a function call. The control structure evaluates the text inside it's parens() in a very different way from how a function call does.
The second thing is... I don't understand what you're trying to say by:
The code you listed for the for loop is a very common structure in C and the variable "i" should, indeed, be available inside the for loop body. Ie, the following should work:
Am I misreading what you're saying?
关于
for
/fgets
混淆的底线是,虽然“封闭大括号控制范围”在大多数情况下是 C 中的正确规则,但还有另一条关于C99 中的作用域(借自 C++)表示在控制结构的序言中声明的变量(即for
、while
、if
)位于结构体的范围内(并且不在结构体之外的范围内)。The bottom line with respect to your
for
/fgets
confusion is that while "enclosing braces control scope" is the correct rule in C most of the time, there is another rule regarding scope in C99 (borrowed from C++) that says that a variable declared in the prologue of a control structure (i.e.for
,while
,if
) is in scope in the body of the structure (and is not in scope outside the body).