Variables in C++
Notes:
Variables in C++ Programming Language :
What is a variable & constant?
Variable:
Variable is a named memory location, whose value can change during the execution of a program.
Constant:
Constant is a named memory location, whose value never changes during the execution of a program.
i.e. constants stay constant whereas variables vary.
W.K.T. While developing any software application using C++; we will be dealing with different types of data and data values. In order to store and process different types of data & data values computer uses its memory (RAM). To allocate a chunk of memory and access it within a program, we need to declare a variable or a constant.
Declaring a variable or a constant: means allocating a memory location for some data
Syntax for declaring a variable:
datatype nameOfVariable; // declaration of a single variable
Ex:
int playerScore;
Note: By default the value of a variable is garbage value (means a meaningless or useless value)
- Having a garbage value inside a variable is considered as one of the bad practice. So while declaring a variable; it is recommended to initialize it with some meaningful value. Initializing a variable while declaring; is considered as one of the best practice.
Initializing a variable or a constant: means assigning the initial value to that allocated memory location
Syntax for declaring and initializing a variable:
datatype nameOfVariable = value; // declaration and initialization of a single variable
Ex:
int playerScore = 0;
Syntax for declaring multiple variables:
datatype nameOfVariable1, nameOfVariable2,...; // declaration of multiple variables
Ex:
int playerScore, playerHealth;
Syntax for declaring & initializing multiple variables:
datatype nameOfVariable1 = value, nameOfVariable2 = value,…; // declaration & initialization of multiple variables
Ex:
int playerScore=0, playerHealth=100;
- In order to change the value in a variable after declaring or declaring and initializing, we need to assign a value to it.
Assigning a variable:
Syntax for assigning value to a variable:
nameOfVariable = newValue; // changing value of a variable
Ex:
playerScore = 10;
playerHealth = 80;
Note:
- In C language; all variables must be declared in the beginning of the scope
- In C++ language; variables can be declared anywhere in the scope
- Before using a variable it must and should be declared
Example Code:
#include <iostream>
using namespace std;
int main()
{
/*
int playerScore;
cout << "player score = " << playerScore << endl; // GIV
int playerHealth;
cout << "player health = " << playerHealth << endl; // GIV
int playerScore = 0;
cout << "player score = " << playerScore << endl; // 0
int playerHealth = 100;
cout << "player health = " << playerHealth << endl; // 100
int playerScore, playerHealth;
cout << "player score = " << playerScore << endl; // GIV
cout << "player health = " << playerHealth << endl; // GIV
*/
int playerScore=0, playerHealth=100;
cout << "player score = " << playerScore << endl; // 0
cout << "player health = " << playerHealth << endl; // 100
playerScore = 10;
playerHealth = 80;
cout << "player score = " << playerScore << endl; // 10
cout << "player health = " << playerHealth << endl; // 80
return 0;
}