C++: Variable declaration and initialization

For clarity in the declaration and use of variables, you can completely use C++ language. If you declare a variable of type string, you cannot assign it in numeric. So you need to know the declaration syntax to use it correctly.

To be able to “Variable declaration and initialization” in C++, we must first declare it, then specify what data type it is. We just need to write the type name (like int, short, float…) followed by a valid variable name.

Syntax

char c;     //character variable declaration.
int area;   //integer variable declaration.
float num;  //float variable declaration.

The first line declares a variable of type Char with the name c. The second line declares a variable of type int with the name area and the same with “float, num”. Once declared, the above variables can be used within their scope in the program.

If you want to declare several variables of the same type and want to save on writing effort, you can declare them on one line, separating the names with commas. 

Integer types (char, short, long and int) can be signed or unsigned depending on the range of values we need to represent.

Declaring More than one Variable

If you want to declare several variables of the same type and want to save on writing effort, you can declare them on one line, separating the names with commas.

int a, b, c;	//more than one variable declaration.

completely equivalent to

int a;  //integer variable declaration.
int b;  //integer variable declaration.
int c;  //integer variable declaration.

Initialization of variables

When declaring a variable, its value is implicitly undefined. But you’ll probably want it to have a definite value when it’s declared. To do that, you just need to write the equal sign and the value you want the variable to carry.

here is syntax: 

type identifier = initial_value;

for example:

int a = 10;  //integer variable declaration & initialization.

practice: 

//Write a CPP program for declaration & initialization of variable
#include <iostream.h>
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;
	return 0;
}

output:

Addition is: 15

Note when declaring variables

A variable declaration provides a guarantee to the compiler and is meaningful only at compile time, the compiler needs to declare the actual variable at link time of the program.

Variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at program link time.

You would use the extern keyword to declare a variable anywhere.

Although you can declare a variable multiple times in your C++ program, it can only be defined once in a file, a function, or a block of code

Conclusion

Above are the basic knowledge needed when variable declaration and initialization, hope our article is useful for you.


Related articles

Scroll to Top