. Advertisement .
. Advertisement .
As far as you know, the C++ programming language is very well-known in software development technology. As a beginner of C++, you should learn about the basic knowledge variable and the way to use it in your program effectively. This post will discuss “variable declaration and initialization” in C++. Let’s continue reading to find useful information!
Overview of C++ Variables
You must store and retrieve data in computing. In the computer’s memory, data is kept. You need to be aware of where the data is kept in the memory in order to retrieve it. You can access data stored in memory with the aid of variables. You can consider them as stand-ins.
A variable in C++ programming is the name provided to a memory region. The memory allotted for a variable houses the value assigned to it. The following is a diagram that illustrates the idea of variables stored in memory:
......... ADVERTISEMENT .........
Let’s examine an illustration to better comprehend the idea of variables:
int age = 14;
Age is a variable of the data type int, and we have given it the integer value 14 in this instance.
Variables can have their values modified, hence the term is the following:
int age = 14; // age is 14
age = 17; // age is 17
Another programming example is a database of employees. Employee name, age, salary, date of hire, and other information can be found in an employee database. To save this information, you would declare the following variables in C++: EmployeeName \sAge \sSalary DateofJoining
Variable declaration and initialization in C++
1. Variable declaration in C++
Declaring One Variable in C++
In C++, if you want to use a variable in your code, that variable needs to be defined firstly with its type. The compiler will remember its size and the way to interpret its value.
To declare a valid variable, we need to determine its types (int, string, float, double, ..) and set the variable and identifier.
char c; //character variable declaration.
int area; //integer variable declaration.
float num; //float variable declaration.
Three variables are declared valid for three types of data:
int (integer), and
float (float). Followed by types, there are identifiers for each type, this identifier depends on the user’s willingness. And don’t forget a semicolon mark when you finish the declaration.
num can be used in the scope of your program after the declaration line.
Declaring more than one Variable in C++
In a program, there are many variables that need to be defined. If it is declared in one line, it may take time for you. To save your effort, it can declare more than one variable in one line as long as they are the same data type.
For example, we want to declare three variables
c, in the same data type is int, typing the code line:
int a, b, c; //more than one variable declaration.
It is completely equivalent to:
int a; //integer variable declaration.
int b; //integer variable declaration.
int c; //integer variable declaration.
2. Initializing variables in C++
When declaring a variable, its value is undetermined until they have been assigned a new value. But you probably want it to have a definite value when it’s declared, you just need to write the equal sign and the value you want. This process is called the initialization of the variable.
Here is the syntax:
type identifier = initial_value;
For example, I initialize a variable a with type int, and value of a is 10.
int a = 10; //integer variable declaration & initialization.
Now, have practice with the require “Write a CPP program for declaration & initialization of a variable”:
//Write a CPP program for declaration & initialization of variable
int main ()
int sum; //Variable declaration
int a = 10; //Variable declaration & initialization
int b = 5; //Variable declaration & initialization
ans = a + b;
cout << "Addition is:" << ans << endl;
The console screen output:
Addition is: 15
Here are some notes for you when declaring and initializing variables in C++.
The compiler will realize that one variable exists with the declared type of data and name (the identifier), and the compiler works without detail about the declared variable.
When the variable is declared, It will have meaning at the time of compilation only. And at the time of linking the program, the compiler also needs actual variable declaration.
The variable declaration is helpful when multiple files are used. Defining the variable in one of the files at program link time will be available to use.
The keyword “extern” is used anywhere in your program when you declare a variable.
Although a variable can be declared many times in your program, it can be only defined once in one file, one function, or one block of code.
This concept for the declaration of variables also applies to function declaration. When a name of a function is declared and its definition will be given in the program.
This post discussed the variable declaration and initialization in C++. Remember that one variable should be declared before initializing it in the program. We hope you enjoy our post! We would appreciate it if you share your thoughts with us via the comment box below! Thank you!