Call: 0123456789 | Email: info@example.com

How to declare and initialize a variable in C++ program


How to declare and initialize a variable in C++ program

How to declare and initialize a variable in C++ program

Welcome to PodinaTutorials.com. In this article we are going to talk about how to declare and initialize a variable in C++ program. In previous article we discussed C Programming Constants and Variables and Naming Rules for variables. Let’s have a look, How to declare a variable in C++.

Declaration of variables

In order to use a variable in C++, we must first declare it. The declaration of a variable specifies that which type of data this variable will store/use. The syntax to declare a new variable is to write the specifier of the desired data type such as int, bool, float etc. followed by a valid identifier. For example:

Data type variable_name;

int a;

When this statement is executed by the CPU, a piece of memory from RAM will be set aside and it will be named as a. More than one variables of the same type can be declared in a single statement by separating them with commas. For example:

int a, b, c;

This line of code declares three variables a , b, ad c, all of type int. This declaration is exactly the same as shown in the following segment of code:

int a;
int b;
int c;

Consider the following example to see the use of variable declaration.

 

#include <iostream.h>
#include <conio.h>
int main()
{
// declaring variables
int a, b;
int result;
// process
a = 55;
b = 22;
result = a – b;
//print out the result
cout << “The Result is = ”<<result;
// Terminate the program
getch();
return 0;
}

Output of the program

The Result is = 33

The declaration of the variables is not necessary at the start of the program. A variable can be declared in the program where it is needed. Consider the following example.

// variable declaration at the point of application
#include <conio.h>
#include <iostream.h>
int main()
{
cout << “Enter a number: ”;
int x; // we need x starting here
cin >> x;
cout << “Enter another number: ”;

int y; // we do not need y until now
cin >> y;
cout << “The sum is: ” << x + y << endl;

getch();
return 0;
}

Output of the program

Enter a number: 6
Enter another number: 11
The sum is: 17

How to declare and initialize a variable in C++ program

Initialization of variables

Assigning values to a variable during declaration time is called initialization. When declaring a regular local variable, its value is by default undetermined. But you may want a variable to store a concrete value at the same moment that it is declared. In order to do that, you can initialize the variable.

Consider the following program to demonstrate the initialization of variables.

#include <conio.h>
#include <iostream.h>
int main()
{
int Vara = 55; // initial value = 55
int Varb =(22); // initial value = 22
int result; // initial value undetermined

result = Vara – Varb;
cout << “The Result is = ”<<result;

getch();
return 0;
}

Output of the program

The Result is = 33

Conclusion

In this article, we discussed how to declare and initialize a variable in C++ program. Declaring a variable is somehow an important concept used in object-oriented programming to save memory and maximize the speed of the program. In next article we will be talking about Some Fundamental data types used in C++ programming.

Also Read  post a comment in c programming language and statement terminator

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *