mechanics of variable scope in c++ -


i had question scope in c++. buddy gave me practice test prepare upcoming technical exam , wasn't sure how answer 1 question.

i'm given code snippet

int nvalue1 = 12, ntotal =0; ntotal += nvalue1; {     int nvalue1 = 14;     ntotal += nvalue1; } ntotal += nvalue1; 

i need refresher on mechanics of scope, because i'm not sure of following answers best.

a) when code goes out of scope, compiler remembers restore value 12.

b) when code goes out of scope, original nvalue1 used again.

i think answer b, right?

the answer neither.

there no "original value", , there nothing remember.

they 2 separate, independent, variables. within inner scope, symbol nvalue1 refers 1 variable. outside of inner scope symbol nvalue1 refers different variable.

even inside inner scope, outer scope's nvalue1 can modified (through pointer, or equivalent mechanism). when inner scope exist, nvalue1 isn't "remembered" have same value did before scope, whatever value updated, indirectly, while inner scope in effect.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -