I noticed a problem when I glanced at my studying document. The problem is about post and pre increment operator ++.
Here are the code in mydocument:
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int k = 0; //global k
int main()
{
int k; //local k
for (k = 0; k < 10; k++) { // LOOK AT HERE !!!!!!!!!!!!!!!!!!!!
cout << " local variable " << k ;
cout << " global variable " << ::k <<endl;
}
::k++;
cout << endl;
cout << " After loop;\n" ;
cout << " local variable : " << k ;
cout << "\n global variable : " << ::k <<endl;
getch();
return 0;
}
Here are the output:
So what if the for loop was that ;
for (k = 0; k < 10; ++k)
Does the output change ?
I expect the same output except for line 1 in the output:
local variable 1 global variable 0
Then,
---------------------------------
local variable 2 global variable 0
local variable 3 global variable 0
...
bla bla bla
...
---------------------------------
Why the k is not 1 in the first looping in for loop ? Or is k++ and ++k the same thing? I know that these are different. But there are as if the same. Can anybody solve this problem in my mind?