Printing Int i in If Portion of C++ Code

  • Context: C/C++ 
  • Thread starter Thread starter Rainier9
  • Start date Start date
  • Tags Tags
    C++ Urgent
Click For Summary

Discussion Overview

The discussion revolves around the behavior of variable scope in C++ and how to print a specific variable value within nested scopes. Participants explore the implications of variable shadowing and the use of the scope resolution operator.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant notes that the declaration "int i = 9" hides the "int i = 5" within the if block, making it inaccessible using the name "i".
  • Another participant suggests that the global variable "int i = 7" will always be referenced unless explicitly specified otherwise, indicating a misunderstanding of variable shadowing.
  • A different participant explains that using the double colon (::) accesses the global variable, highlighting the function of the scope resolution operator.
  • There is a suggestion to declare the variable without an initializer to avoid confusion, although this does not directly address the original question.

Areas of Agreement / Disagreement

Participants express differing views on the implications of variable shadowing and the behavior of the scope resolution operator. No consensus is reached on the best approach to achieve the desired output.

Contextual Notes

Limitations include the potential for confusion regarding variable scope and shadowing, as well as the specific behavior of the scope resolution operator in different contexts.

Rainier9
Messages
32
Reaction score
0
I need to print the int i right next to the main, but I need to do it while I am in the if portion of the following code:

Code:
int i = 7;
int main()
{
int i = 5;
cout << ::i;
if(1)
{
int i =9;
cout << ::i<<endl;
}

    return 0;
}

Right now it prints 7. I need it to print 5 without changing the variable name.
 
Technology news on Phys.org
Rainier9 said:
I need to print the int i right next to the main, but I need to do it while I am in the if portion of the following code:

Code:
int i = 7;
int main()
{
int i = 5;
cout << ::i;
if(1)
{
int i =9;
cout << ::i<<endl;
}

    return 0;
}

Right now it prints 7. I need it to print 5 without changing the variable name.

Hi Rainier9! :smile:

The "int i=9" hides the "int i=5" in a way that it is inaccessible using the name "i".
The language has been designed this way.
 
You declare "int i = 9" as a global before main() and then try to name a new "int i" that is set to 5. I am surprised you didn't get at least a warning about it when you compiled.
The global int i will always be processed for any statement or function that calls i.
To fix it just declare i. like this: "int i"
then your program should be fine.
Paul
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
12
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 5 ·
Replies
5
Views
8K