C++C++11C++14C++17Introduction to C++Learn C++

What Is The Purpose Of Local Variables In C++?

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.

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,

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,

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.

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.

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.

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().

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.

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.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

How To Make Use Of Wide String Properties In C++ Software

C++C++11C++14C++17Introduction to C++Learn C++

What Is The find() Wide String Method In A Modern C++ App?

C++C++11C++14C++17Introduction to C++Learn C++

How To Make find_first_not_of() String Searches In A C++ App

C++Introduction to C++Language FeatureLearn C++

How To Use The ExtractFileName Method In Your C++ Software