You have to understand what is a local variable.
b and c are local to their respective functions, they exists only when the code reaches their declaration, and they are gone as soon as the block they are in is exited (here, at the end of the function).
That's the point of local variables: they are not visible outside their scope. You could have named c as b, without change of behavior. You could even name them a, they would have hidden the global variable, no change there.
To change this, there is several tactics.
- Make them global. Risky (can be changed by some function without knowing it), unpractical (need a distinct name per variable), etc.
- Make one function to return the value of the local variable: int ex_2() { int c = 2; println(c); return c; } then void ex_1() { int b = 1; println(ex_2()); }
- Use classes: the values of their variables (fields) are persisted as long their objects live, and can be easily accessed.