C ++全局和局部变量 [英] C++ global and local variables

查看:141
本文介绍了C ++全局和局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下代码:

#include<iostream>
using namespace std;
int i = 1;
int main(int argc,char ** argv)
{
    int i = i;
    cout<<i<<endl; // which i?
    return 0;
}

它可以通过编译,但是给出错误的答案, ?

It can pass the compile, but gives the wrong answer, how to explain this?

推荐答案

本地变量是可访问的,
类似于调用两个具有相同名字的人,和一个房间外面。
在您试图访问它的范围内的人,听到它。

the Local variable is accessible, analogous to calling two people with a same name, one inside the room, and one outside the room. The one who is in the scope you are trying to access it , hears it.

这篇关于C ++全局和局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆