In this article, you’ll learn what global variables are in programming, what a local variable is in programming, how to use local variables in C++, and what the difference between local and global variables is. By learning C++ variables, you can build C++ applications easily with C++ IDE.
Table of Contents
Why is the placement of a variable definition important?
The placement of variable definitions is very important, you can define a global variable or a local variable. Generally, they should be declared before all, at least before the usage. You can declare all data types (int, float, double, string, UnicodeString, TBitmap etc) as a global variable or local variable. Global Variables are defined in the main code of any functions, generally after headers or before the main()
function, and they can be used in any function inside. Local Variables are only used inside the function that they were declared.
What is a Global variable?
In C and C++ programming, a Global Variable means it is a variable that can be used in any lines, any methods or functions or any scope of codes. The Global Variable is defined in the main code lines out of any functions, generally after headers or before the main()
function, and they can be used in any function inside. A global variable takes place in the memory since the application is done.
What are Local variables?
In C and C++ programming, a Local Variable means it is a variable that can be used inside that function or method or in scope. Local Variables can be only used inside the function or method or a scope that they were declared. They cannot be used in other methods or functions. They are destroyed when the method or function is done. This means a local variable only takes place in the memory when its local scope is running.
Is there an example of how to use global and local variables together?
Let’s understand global and local variables together in an example. For example, if you define int gx = 15; before the main()
or outside of any function, it is a global variable, if you define int lx = 20; inside a function i.e. in main()
function it is a local variable. These can be float, bool, double, string or any type defined by user. See this example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> int gx = 15; // this is global variable int main(int argc, char** argv) { int lx = 25; // this is local variable works only in this procedure std::cout << "global variable gx=" << gx << '\n'; std::cout << "local variable lx=" << lx << '\n'; return 0; } |
Is there an example of using local variable types ?
We can define all kinds of data types a local variable inside a function or method. We can define local variables of the main function inside the main()
scope as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> int main() { int local1 = 245; float local2 = 20.22; double local3 = 4.041; std::string local4 = "This is a Local String"; std::wstring local5 = L"This is a Local Wide String"; return 0; } |
Is there an example of what is meant by Local Scope?
Local Scope is a local coding area that local variables are removed from the memory when it is done and it is defined as between the { and } characters. We can use { and } to define local scopes (a specific local coding areas inside a function or a method, etc.). For example in this example below x is a local variable its scope and if we try print out this x value outside of the scope we get error in compilation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> int main() { { // Local Scope int x = 100; } std::cout << x; // ERROR: use of undeclared identifier 'x' return 0; } |
Is there an example of using local variables in a loop?
For
loops and do()-while
loops are local scopes. Any variable declarations inside are local variables they cannot be used outside of them. For example, in the code below we get errors when we try to printout the contents of the i and k variables because they are local variable inside the for
loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> int main() { for(int i = 0; i<100; i++) { int k =5; } // i and k removed from the memory here std::cout << i ; // ERROR : use of undeclared identifier 'i' std::cout << k ; // ERROR : use of undeclared identifier 'k' getchar(); return 0; } |
In this example below we can print out both i and k. Note that k is defined in every loop and removed from the memory at the end of one loop cycle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> int main() { for( int i = 0; i < 100; i++) { int k =5; std::cout << i << ',' << k << std::endl; } getchar(); return 0; } |
Can we have an example of using the same variable name for different local variables?
Let’s see two different local variable definitions. First a str variable is defined in the main function and a second is defined in a function called local_test()
.
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> #include <string> void local_test() { std::string str = std::string("This is a Local Test in The Function"); std::cout << "Step2:" << str << std::endl; } int main() { std::string str = "This is a Local Test in The Main Function"; std::cout << "Step1:" << str << std::endl; local_test(); std::cout << "Step3:" << str << std::endl; getchar(); return 0; } |
If we run this example, first it will print out the local variable str in the main string then when we run local_test()
function it will print out the str inside the local. At the end of this function this will delete the local str variable of the local_test()
. At the third step if we try to print out str again it will print out the same str of the main function.
1 2 3 4 5 |
Step1:This is a Local Test in The Main Function Step2:This is a Local Test in The Function Step3:This is a Local Test in The Main Function |
As you see two different local variables definitions with the same name are independent in their scope. Because of this when you are using local variables try to use different names to make them understandable and to avoid some wrong usage in some cases.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition