Local variables : are used within a block such as
int main()
{
int num = 20;
cout >> “please enter a number” >> endl;
cin << num << endl;
}
Or in a function (maybe)
Int number (int num)
{
Cin << num
}
This sets num to 20 for the main() function
They cannot be used outside of their block they are initialized in.
We have used these in just about every program so far.
If we think of them as a tv show, they are characters that come in for one episode or the main theme of one episode.
Global variables: These are variables that are initialized before the main program body
Example:
#include
using namespace std;
int num = 20;
int main()
{
cout << num << endl;
This initializes num to 20 for the entire program.
To use the tv example, they are the main characters or main plot of the show
We have used them, not as much though
Static variables: These are almost the exact same thing as global variables, except you can declare them within a block by using:
Static datatype identifier
Or an example
Static int = 20;
Another thing to note is that as long as the program is “going”
A good example is X on page 392
Void test()
Static int x = 0;
X = X + 2;
Cout << “inside test x = “ << x << endl;
Inside test x = 2
Inside test x = 4
Inside test x = 6
Ecrt..
What this does is increase x by 2 each time it executes around .
We have not used these that I am aware of.
Automatic variables: The book describes these as pretty much the same as local variables. The only addition from local variables that is mentioned is that they are initialized to 0.
From other wibsites that I found they said that they are not normally used by “human” programmers but program generators