简体   繁体   English

C99:我可以在“ for”的块开头声明变量吗?

[英]C99: Can I declare variables in the beginning of a block in a 'for'?

Is the following code legal according to C99? 根据C99,以下代码是否合法?

...
for(....) {
int x = 4;
...
}
...

You can assume that before line 3 the variable x was never declared. 您可以假设在第3行之前从未声明过变量x。

C99 (PDF) C99(PDF)

Until now I have only found the following, but I dont think that this is enough: 到目前为止,我仅发现以下内容,但我认为这还不够:

A block allows a set of declarations and statements to be grouped into one syntactic unit. 块允许将一组声明和语句分组为一个语法单元。 The initializers of objects that have automatic storage duration, and the variable length array declarators of ordinary identifiers with block scope, are evaluated and the values are stored in the objects (including storing an indeterminate value in objects without an initializer) each time the declaration is reached in the order of execution, as if it were a statement, and within each declaration in the order that declarators appear. 每次声明以下内容时,都会对具有自动存储持续时间的对象的初始化程序以及具有块范围的普通标识符的可变长度数组声明符进行评估并将其值存储在对象中(包括在没有初始化符的对象中存储不确定的值)。以执行顺序(就像是一条语句)到达,并且在每个声明中以声明者出现的顺序到达。

From page 145 of that PDF. 摘自该PDF文件的第145页。

This is legal in both C99 and C89. 这在C99和C89中都是合法的。 Look at 6.8.2 , which defines compound statement 看6.8.2,它定义了复合语句

Yes, you can declare or define a variable anywhere you want in C99 (at the start of a block in C89). 是的,您可以在C99中任何需要的地方声明或定义一个变量(在C89块的开头)。

You said: 你说:

"You can assume that before line 3 the variable x was never declared." “您可以假设在第3行之前从未声明过变量x。”

Even if it was previously declared, you could declare a new variable with the same name. 即使先前已声明它,也可以声明一个具有相同名称的变量。 Doing that prevents you from accessing the old variable within that block. 这样做会阻止您访问该块中的旧变量。

int x = 0;               /* old x */
printf("%d\n", x);       /* old x, prints 0 */
do {
    int x = 42;          /* new x */
    printf("%d\n", x);   /* new x, prints 42 */
} while (0);
printf("%d\n", x);       /* old x, prints 0 */

I've never tried the following in C99. 我从未在C99中尝试过以下方法。 I really don't know what happens :) 我真的不知道会发生什么:)
I'll try later, when I get access to a (almost) C99 compiler 我将在稍后(几乎)可以使用C99编译器时尝试

int x = 0;
do {
    printf("%d\n", x);   /* old x? new x? crash? Undefined Behaviour? */
    int x = 42;
} while (0);

The C99 feature of declaring/defining variables wherever one wants is not a feature that makes me want to change :) 声明/定义变量的C99功能并不是让我想要更改的功能:)

Yes, you can create a variable at the beginning of any block. 是的,您可以在任何块的开头创建一个变量。 The variable is initialised each time the block is entered In C++, you can create them anywhere within the block. 每次在C ++中输入该块时都会初始化该变量,您可以在该块中的任何位置创建它们。

for(....)
{
  int x=4;
  /*More code*/
}

Yeah this is legal in C99 but you are not allowed to access 'x' after the block.It would be Undefined Behaviour trying to access 'x' beyond its scope. 是的,这在C99中是合法的,但是不允许在块之后访问'x',这是试图访问其范围之外的'x'的未定义行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM