Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Variable Scope

Recommended Posts

Below is my code, which pretty much follows the Ivor Horton book on learning C++.

My question is this: Why does the program return the value of "80" for count3 in the last cout statement? Why doesn't it return a value of "50", since the cout statement is located in the outer block and so should reference the value made in the initializiation made near the top of main().

What am I not understanding here? Thanks.

//ex2_06.cpp//demonstrating variable scope#include<iostream>using namespace std;int count1 = 100; //Global version fo count1int main(){ //function scope begins here int count1 = 10;int count3 = 50;cout << endl<< "Value of outer count1 = " << count1<< endl;cout << "Value of global count1 = " << ::count1 //From outer block<< endl;{ //new scope starte here...int count1 = 20; //This hides the outer count1int count2 = 30;cout << "Value of inner count1 = " << count1<< endl;cout << "Value of global count1 = " << ::count1 //From outer block<< endl;count1 += 3; //This affects the inner count1count3 += count2;} //...and ends herecout << "Value of outer count1 = " << count1<< endl<< "Value of outer count3 = " << count3<< endl;//cout << count2 << endl; //uncomment to get an errorreturn 0;} //Function scope ends here

Notice from snlildude87:
Code goes inside the code box. Quote goes inside the quote box. You don't have a problem with the latter, so put all your codes inside the code box. Thank you.

Edited by snlildude87 (see edit history)

Share this post


Link to post
Share on other sites

The reason i think it displays 80 instead of 50 is because of one reason. You cannot have brackets unless it is for a class or structure or function definition or unless the statement is a while, for, if, etc. statement. THerefore, where you say

{ //new scope starte here...

you should get an error or it just ignores them and stays with the same scope. Try it with an if statement to understand what i mean.

Share this post


Link to post
Share on other sites

ffanatics, nope. You can have parentheses wherever you like, as long as you close any parentheses you open.

The reason you get 80 is because you added 30 to it... it's no trick.

Whenever you declare a new scope within a previous... e.g.

int count1 = 10int count3 = 50;{  // New scope    // Declare a variable called count1 (this means that when referring     // to count1 the count1 referred to is the one in this scope, and the     // one in the outer scope is ignored    int count1 = 20;        // Because no new variable, count3 has been declared here, count3    // from the outer scope is referred to    count3 += count1;} // Close scope

You'll notice in the example you gave, count3 is not initialized in the new scope either, so when that value is used, it refers to the first declaration of count3.

Share this post


Link to post
Share on other sites

hi,yeah. that is the answer.You are adding count2 to count3 in the inner block.But since count3 is not declared in the inner block, it affects the count3 variable in the outer scope and hence 30 is added to the existing value of 50. This gives the answer as 80.you would understand it better if you just output count3 in the inner block.Cheers.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.