Add to Google

Variable Scope

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

Variable Scope

kvarnerexpress
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.

CODE
//ex2_06.cpp
//demonstrating variable scope
#include<iostream>

using namespace std;

int count1 = 100; //Global version fo count1

int 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 count1
int 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 count1
count3 += count2;
} //...and ends here

cout << "Value of outer count1 = " << count1
<< endl
<< "Value of outer count3 = " << count3
<< endl;

//cout << count2 << endl; //uncomment to get an error

return 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.

 

 

 


Reply

fffanatics
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
QUOTE
{ //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.

Reply

dexter
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.

CODE

int count1 = 10
int 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.

 

 

 


Reply

cse-icons
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.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.


*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for variable, scope

*MORE FROM TRAP17.COM*
advertisement



Variable Scope



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE