Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Variable Scope
kvarnerexpress
post Jun 27 2005, 08:42 PM
Post #1


Super Member
*********

Group: Members
Posts: 407
Joined: 13-December 04
Member No.: 2,696



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.


This post has been edited by snlildude87: Jun 27 2005, 09:38 PM
Go to the top of the page
 
+Quote Post
fffanatics
post Jun 27 2005, 09:44 PM
Post #2


Privileged Member
*********

Group: [HOSTED]
Posts: 936
Joined: 14-April 05
From: West Chester, PA
Member No.: 5,636



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.
Go to the top of the page
 
+Quote Post
dexter
post Jun 28 2005, 05:29 AM
Post #3


Advanced Member
*******

Group: Members
Posts: 142
Joined: 24-December 04
From: Queensland, Australia
Member No.: 2,902



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.
Go to the top of the page
 
+Quote Post
cse-icons
post Jun 28 2005, 01:36 PM
Post #4


Super Member
*********

Group: Members
Posts: 351
Joined: 19-October 04
From: India
Member No.: 1,824



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.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Free Memory After Out Of Scope?(1)
  2. Calling A Variable From A Dynamic List In A Sql Statement(2)
  3. Stored Or Global Variable?(1)
  4. Php Parse Error(2)
  5. Calling A False Variable...(6)
  6. Setting The Height Of A Tablerow(4)
  7. Determining Whether A Phrase Is In A Variable(2)
  8. Hosting Credits Php Variable?(0)
  9. Limiting Usage Of Variable Combination(1)
  10. Variable Definition(6)
  11. <textarea> Tag Is Not Accepting Php Variable(2)
  12. Converting Characters In A Variable To Individual Values In An Array(2)
  13. Php Form Help!(3)
  14. Javascript Help(1)
  15. Getting An Array Value Of A Dynamic Variable(9)
  1. Php Echo(10)
  2. Php Variable Basics(9)
  3. Script Help Required: Undefined Variable(3)
  4. Game Dev Team(4)
  5. Unexpected T_variable...(3)
  6. Vb Variable Help(2)


 



- Lo-Fi Version Time is now: 13th October 2008 - 04:42 PM