Article by: Manish Methani
Last Updated: October 21, 2021 at 2:04pm IST
Variables are fundamental to programming in C++. They allow programmers to store and manipulate data in their programs. In this tutorial, we will provide a comprehensive guide on variables in C++. We will cover what variables are, how to declare and define them, and how to use them in your code.
In C++, a variable is a named location in memory that holds a value. The value stored in the variable can be changed during program execution. Variables are used to store various types of data, such as integers, floating-point numbers, characters, and more.
To use a variable in C++, you need to declare and define it. Declaration informs the compiler about the name and type of the variable, while definition allocates memory for the variable. Here is an example:
int age; //declaration age = 25; //definition
In this example, we declare a variable called age
of type int
. We then define age
to have a value of 25
. You can also declare and define a variable at the same time, like this:
float height = 5.7; //declaration and definition
In this example, we declare and define a variable called height
of type float
, with an initial value of 5.7
.
Once you've declared and defined a variable, you can use it in your program. Here is an example:
#include <iostream> using namespace std; int main() { int age = 25; cout << "My age is " << age << endl; return 0; }
In this example, we declare and define a variable called age
of type int
with a value of 25
. We then print out the value of age
using cout
.
Local variables are defined within a function or a block of code and can only be accessed within that function or block. Once the function or block ends, the local variable is destroyed. Here is an example:
#include <iostream> using namespace std; int main() { int num = 10; // local variable cout << "The value of num is " << num << endl; return 0; }
In this example, we define a local variable num
of type int
with a value of 10
. We then print out the value of num
using cout
.
2. Global Variables:
Global variables are defined outside any function and can be accessed by any function or block of code within the program. They have a lifetime that extends throughout the entire program. Here is an example:
#include using namespace std; int num = 10; // global variable int main() { cout << "The value of num is " << num << endl; return 0; }
In this example, we define a global variable num
of type int
with a value of 10
. We then print out the value of num
using cout
.
3. Static Variables:
Static variables are declared with the keyword static
. They retain their value between function calls and have a lifetime that extends throughout the entire program. Here is an example:
#include <iostream> using namespace std; void count() { static int num = 0; // static variable num++; cout << "The value of num is " << num << endl; } int main() { count(); count(); count(); return 0; }
In this example, we define a static variable num
of type int
with an initial value of 0
within the function count()
. We then call count()
three times and increment the value of num
by 1
each time. We then print out the value of num
using cout
.
In conclusion, variables are a crucial part of programming in C++. They allow programmers to store and manipulate data in their programs. In this tutorial, we've covered the basics of declaring and defining variables, as well as how to use them in your code. By mastering variables in C++, you'll be well on your way to becoming a proficient programmer.