From the course: Complete Guide to C++ Programming Foundations

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Using variables

Using variables

- [Tutor] Let me show you an example of variable declaration and usage for several types. Variable declarations must include the type and name of the variable, and they may include an initialization. So let me go to line seven and declare two integer variables a and b, and let me initialize b to five. Notice that you may declare several variables in one line, separating them with commas. Also, notice that these two variables are at the same level of indentation as the main function. This means that a and b are global variables. Global variables are accessible to all parts of the code. Because of this, the memory for global variables is managed statically by the compiler, so they are allocated in the data segment of memory. Once the program ends, their memory is freed. On the other hand, variables may be local to what is known as their scope. You may declare variables inside functions or inside loops, which de limit their scopes with curly brackets. These variables are accessible in…

Contents