English 中文(简体)
C++中可变声明
原标题:Variable declaration in c++
  • 时间:2011-11-17 05:34:30
  •  标签:
  • c++

when i run the following c++ code

#include<iostream>

int main()
{
   for(int i=1;i<=10;i++)
   {
       cout<<i<<endl;
   }
   cout<<i;

   getch();
   return 0;
}

我得出了第1至11号结论。

i don t understand why the value of i = 11 after the block of for loop is finished,Please give me the reason.I have declared i inside for loop and scope of i has been finished after loop so why i get the outpout i=11 after execute second cout statement .I have not declared i in the variable declaration inside main.My question is that is i is visible outside of for loop? Thanks in advance.

最佳回答

由于多种原因,该方案没有汇编成册。 你们要么使用一个极其古老的汇编者,一个极其宽容的汇编者,要么没有向我们表明你实际上存在问题的方案。

From your comments it seems that you actually can compile it. I can only guess that you are using a very old compiler. Perhaps an old MS-DOS compiler (Zortech C++? Turbo C++?) since the getch function is not generally a standard library function and doesn t do the right thing in the curses library anyway. It s probably an old BIOS-based function from the MS-DOS days.

The standard was changed awhile ago (over 10 years now) so that variable declarations in the parenthesized section of a for loop are local to that loop. It was once not actually the case that this was true.

I no longer have access to any compiler that s so old it doesn t handle things this way. I m surprised anybody does. Your program will not compile on my compiler.

本文是你汇编的方案文本,尽管它要求<代码>-lcurs选项链接:

#include <iostream>
#include <curses.h>

using ::std::cout;
using ::std::endl;

int main()
{
    for(int i=1;i<=10;i++)    
    {
        cout<<i<<endl;
    }

    getch();
    return 0;
}

Notice how the offending cout << i; statement is gone? That because it will not compile on a modern compiler.

现在,请你将方案编辑成<代码>cout << i;。

#include <iostream>
#include <curses.h>

int main()
{
    using ::std::cout;
    int i;

    for (i = 1; i <= 10; i++)
    {
        cout << i <<  
 ;
    }
    cout << "last: " << i <<  
 ;

    getch();
    return 0;
}

This, of course, does print out last: 11 at the very end. This happens for a very obvious reason. What value does i have to have in order for the i <= 10 test to fail? Why, any value greater than 10! And since i is having one added to it every loop iteration, the first value i has that has the property of being greater than 10 is 11.

lo格检测是在 lo顶端进行的,用来决定 lo余部分是否应当执行。 而这种增益则发生在 lo底处(尽管出现在<<>>for>上)。 因此,<代码>i将刊印10,然后加添1。 然后将测试(i <=10),将发现11 <=10是假的,控制将在休息室后排在纸面上,last:11将印制。

是的,在C也会发生同样的事情。

问题回答

由于条件<代码>i<=10成为untrue<>/em>时的休息时间,可在<代码>i上<11>>。 简单!

我认为,你希望撰写<代码>i < 10。

此外,正如评论中所指出的,该法典甚至汇编成册,因为<代码>i在册外有t。 或许,你在原法典中宣布了“i,不在此列?

除了编印本(because i)之外,在编外还未加。

休息时间从1到10年,因此,休息时间从i到11年,条件已经失效。 因此,在休息期结束时,<>i为11。

这是因为你有一位老的编辑。

<代码>cout<<i<<endl;不应加以汇编,因为<代码>cout和endl需要由<编码>std 名称空间加以限定。

Fixing that, std::cout<<i; shouldn t compile because your variable is loop-scoped, so shouldn t even be visible outside the loop.

总结如下:

#include<iostream>
#include<conio.h>

int main()
{
    int i;
    for(i = 1; i <= 10; i++)
    {
        std::cout << i << std::endl;
    }

    std::cout << i;

    getch();
    return 0;
}

更清楚的是,为什么现在印发<代码>11。

i = 10时,休息室执行、加添<代码>i并检查其价值。 然后,它等于11,这样它就离开了通道。

之后,你还有一份印刷说明,将打印后版价值,即<代码>11。

这里,我从经更正的方案获得产出:

1
2
3
4
5
6
7
8
9
10
11

这同你一样。

如果你只想印刷<代码>1-10,那么为什么有额外的<代码>:cout << i;?

<>建议>

  • Get an up to date C++ compiler that will give you syntax errors on things that are no longer valid in standard-compliant C++
  • Get rid of the extra std::cout << i;
  • Keep your i variable loop-scoped, as in your original code

我的建议的结果是,你只见1>,到10印刷,你今后会略少出意外(因为有些“坏”和令人惊讶的代码甚至汇编)。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?