-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalVariables.cpp
More file actions
24 lines (19 loc) · 1000 Bytes
/
LocalVariables.cpp
File metadata and controls
24 lines (19 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
// Scope of a variable: where a variable is acessible (globally or locally) and also its lifetime.
long long int X = LONG_MAX; // Global variable, acessible through all CPP file.
int main()
{
int I;
/* This variable is acessible through all main function.
If I create another function I can only access this value if I send it to the function as a parameter. */
std::cout << "Global varible value = " << X << "\n";
for (I = 0; I < 5; I++)
{
int J = I + 2; // This variable is only acessible inside the for loop.
std::cout <<"i = " << I << " | j = " << J << " | I + J + X = " << I + J + X << "\n";
} /* Despite the maximum value of a long long int is declared, the sum(I + J + X) will still show its result
because it's not inside a variable */
std::cout << "Final I value = " << I << "\n";
// std::cout << "Final J value = " << j << "\n"; This line is unable to show J value because it no longer exists.
std::cout << "Global varible value = " << X << "\n";
}