5x and 6x??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <conio>

void a(void);
void b(void);

int x=1;
int linenum = 0;

main(){
	int x=5;

   linenum++;
   cout<<linenum<<"x = "<<x<<endl;
   {
   	int x=7;
      	linenum++;
      cout<<linenum<<"x = "<<x<<endl;
   }
   linenum++;
   cout<<linenum<<"x = "<<x<<endl;

   a();b();

   linenum++;
   cout<<linenum<<"x = "<<x<<endl;

   getch ();
   return 0;
}

void a(void){
	int x=25;

   linenum++;
   cout<<linenum<<"x = "<<x<<endl;
}

void b(void){
	linenum++;
   cout<<linenum<<"x = "<<x<<endl;
}



1x = 5
2x = 7
3x = 5
4x = 25
5x = 1
6x = 5


Hi, can I know why the output give 5x=1 and 6x=5? I'm not really understand how to trace this program one by one...Can anyone help? Thanks...
the 5th output comes from the call to b(); b() doesn't have a local x defined so it takes the global x which you defined as 1. The last output just takes the x local to main() which is still 5;

[code]
1x = 5 // x is local to main
2x = 7 // x is local to bracketed segment inside main
3x = 5 // x is local to main
4x = 25 // x is local to a()
5x = 1 // x is global
6x = 5 // x is local to main
[code]
Thanks...

may I know what u mean by local and global? How to identify which one is local and global?

next question is, 4x = 25 // x is local to a() how to know that is only local to a() but not b()? TQ
http://cplusplus.com/doc/tutorial%20%20/namespaces/

You can't work with functions unless you understand the concept of scope. Start with the tutorial page above and then do some searching. There's no point asking for an explanation here as namespaces are common to most computer languages so there are thousands of tutorials on the subject readily available.
The "global" x is so because it is declared outside of all of the other functions and organizational blocks. The x inside main is "local" to main because functions outside of main cannot access it. The x declared in a() is created when a() is called and destroyed when a() returns so nothing outside of a() can access it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int x = 0; // global variable can be accessed by any function in this unit (cpp file)

void alter_global(int x) { ::x = x; } // assigning global x the value of x passed to function

void print_x(int x) { std::cout << x << std::endl; } // printing x passed to function

void local_function() { int x = 10; print_x(x); print_x(::x); } // printing local x then global x (lines 4 and 5 on output)

int main()
{
  int x = 5;
  print_x(x); // print local x (line 1 on output)
  print_x(::x); // print global x (line 2 on output)
  alter_global(99); // change global x to 99
  print_x(::x); // print global x (line 3 on output)
  local_function();
  return 0;
}


The output for the code above is this:
1
2
3
4
5
6
7
8
5
0
99
10
99

Process returned 0 (0x0)   execution time : 0.047 s
Press any key to continue.
Last edited on
Thanks Texan...More understand now...

Can I know why u r using print_x instead of printf or scanf?

And I saw some program have both like printf and cout, like the one u used...what's the benefit of using different type of it?

TQ...
Topic archived. No new replies allowed.