简体   繁体   English

在for循环中声明和初始化变量

[英]Declaring and initializing variable in for loop

Can I write simply 我可以写简单吗?

for (int i = 0; ...

instead of 代替

int i;
for (i = 0; ...

in C or C++? 在C或C ++?

(And will variable i be accessible inside the loop only?) (并且变量i只能在循环内访问吗?)

Its valid in C++ 它在C ++中有效

It was not legal in the original version of C. 它在C的原始版本中是不合法的。
But was adopted as part of C in C99 (when some C++ features were sort of back ported to C) 但是在C99中作为C的一部分被采用(当某些C ++特性被反向移植到C时)
Using gcc 使用gcc

gcc -std=c99 <file>.c

The variable is valid inside the for statement and the statement that is looped over. 该变量在for语句和循环语句中有效。 If this is a block statement then it is valid for the whole of the block. 如果这是一个块语句,那么它对整个块都有效。

for(int loop = 0; loop < 10; ++loop)
{
    // loop valid in here aswell
}

// loop NOT valid here.

是的,它在C ++和C99中是合法的。

It's perfectly legal to do this in C99 or C++: 在C99或C ++中执行此操作是完全合法的:

for( int i=0; i<max; ++i )
{
    //some code
}

and its while equivalent is: while当量为:

{
    int i=0
    while( i<max )
    {
        //some code
        ++i;
    }
}

Acutally for(int i=0;i<somevalue;i++) was always drilled into me as the preferred way to define a for loop in c and c++. 实际上for(int i=0;i<somevalue;i++)总是钻进我作为在c和c ++中定义for循环的首选方法。

As far as "i" only being accessible in your loop, you have to be care about the variable names you use. 至于“i”只能在循环中访问,您必须关心您使用的变量名称。 If you declare "i" as a variable outside of the loop and are using it for something else then you are going to cause a problem when using that same variable for a loop counter. 如果您将“i”声明为循环外的变量并将其用于其他内容,则在将相同的变量用于循环计数器时会导致问题。

For example: 例如:

int i = 10;
i = 10 + PI;

will be automatically changed when you hit the for loop and declare i=0 当您点击for循环并声明i = 0时,将自动更改

Yes and yes. 是的,是的。 But for C, apparently your compiler needs to be in C99 mode. 但是对于C,显然你的编译器需要处于C99模式。

Can I write simply 我可以写简单吗?

Yes. 是。

(And will variable i be accessible inside the loop only?) (并且变量我只能在循环内访问吗?)

Depends on the compiler and its' version. 取决于编译器及其版本。 AFAIK, in modern compilers i is accessible inside of the loop only. AFAIK,在现代编译器中,我只能在循环内部访问。 Some older compilers allowed i to be accessible outside of loop as well. 一些较旧的编译器也允许我在循环之外访问。 Some compilers allow i to be accessed outside of the loop and warn you about non-standard behavior. 有些编译器允许在循环外部访问i,并警告您非标准行为。

I think (but I'm not sure about it), that "i outside of the loop" was used somewhere in VC98 (Visual Studio 6, which AFAIK, also had a globally defined "i" variable somewhere that could lead to an extremely interesting behavior). 认为 (但我不确定),“我在循环之外”是在VC98中的某个地方使用的(Visual Studio 6,其中AFAIK也有一个全局定义的“i”变量,可能导致极端有趣的行为)。 I think that (microsoft) compilers made somewhere around around 2000..2003 started printing "non standard extensions used" for using i outside of loop, and eventually this functionality disappeared completely. 认为 (微软)编译器大约在2000年左右开始编写。2003年开始打印“非标准扩展使用”在循环外使用i,最终这个功能完全消失了。 It isn't present in visual studio 2008. 它在visual studio 2008中不存在。

This is probably happened according to a standard but I cannot give a link or citation at the moment. 这可能是根据标准发生的,但我现在无法给出链接或引用。

if you use variable out side the loop it will be change every time when you initialize it inside loop 如果你在循环中使用变量,那么每次在循环内初始化时它都会改变

int i = 0;
for(int e = 0; e < 10; e++)
{
 i = e;
}

now i value will change every time 现在我的价值每次都会改变

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

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