C++ Variable : Declare and Assign

C++ Variable : Declaration and Assignment

What is variable ?

Variable reserve space in memory to store the data. You can change the data you stored in the variable. Since the data could vary, it is called variable.
When declaring variable you should specify what type of data it should store and how much memory it should reserve for it. The following table shows the type of data and size of memory it reserve for that data and also how many values you can store in that data type.


Type
Size
Value
bool
1 byte
True or false
unsigned short int
2 bytes
0 to 65535
short int
2 bytes
-32768 to 32767
unsigned long int
4 bytes
0 to 4,294,967,295
long int
4 bytes
–2,147,483,648 to 2,147,483,647
int (16 bit)
2 bytes
–32,768 to 32,767
int (32 bit)
4 bytes
–2,147,483,648 to 2,147,483,647
unsigned int (16 bit)
2 bytes
0 to 65,535
unsigned int (32 bit)
4 bytes
0 to 4,294,967,295
char
1 byte
256 character values
float
4 bytes
1.2e–38 to 3.4e38
double
8 bytes
2.2e–308 to 1.8e308


Note : The size of some variable type will differ on 16-bit and 32-bit processor.
Imp* : You can use "short" instead of "short int" and "long" instead of "long int".

On the 32-bit processor the value of short ranges from –32,768 to 32,767 but if you declare unsigned short the value ranges from 0 to 65535. You cannot store negative value in unsigned. Never declare unsigned before variable type if there is any possibility to store a negative value.

How to declare a variable ?

To declare a variable first type the variable-type (also known as data type) followed by the variable name and then teminate it by semicolon.
For example :
int number;
In the above example, int is the data type and number is name of variable. You can give any name to the variable except for name used for keywords. Variable name can contain letters, numbers or underscore. But the first character should always be either letter or underscore.

How to assign value to a variable ?

There are two ways to assign value to a variable :
1) Assign the value directly in the program.
2) Ask from user to input a value and then assign that value.

A C++ program example that assign the value directly in the program.


#include <iostream>

int main ()
{

    using std::cout;
    using std::endl;

    int a = 10, b = 20;
    int sum = a + b;

    cout << "Addition is : " << sum;

    return 0;
}


A C++ program example that ask the user to input a value then assign that value.


#include <iostream>
#include <iomanip>

int main ()
{

    using namespace std;

    int a, b, sum;

    cout << "Enter two number for addition." << endl;

    cout << "First" << setw (3) << ": ";
    cin >> a;

    cout << "Second" << setw (1) << ": ";
    cin >> b;

    sum = a + b;
    cout << "Addition is : " << sum;

    return 0;
}


A C++ program example that demonstrate the declaration and assignment of various data types.


#include <iostream>
#include <iomanip>

int main ()
{

    using namespace std;

    short myShort = 2000;
    int myInt = 200000;
    long myLong = 5000000;
    float myFloat = 1255.549;
    double myDouble = 78079.3;
    char myChar = 'a';
    char myString1[20] = "I love ";
    string myString2 = "C++ Programming";

    cout << "myChar" << setw (5) << ": " << myChar << endl;
    cout << "myShort" << setw (4) << ": " << myShort << endl;
    cout << "myInt" << setw (6) << ": " << myInt << endl;
    cout << "myLong" << setw (5) << ": " << myLong << endl;
    cout << "myFloat" << setw (4) << ": " << myFloat << endl;
    cout << "myDouble" << setw (3) << ": " << myDouble << endl;
    cout << "myString1" << setw (1) << ": " << myString1 << endl;
    cout << "myString2" << setw (1) << ": " << myString2 << endl;

    return 0;
}


C++ Notes:

(1)As we know that in english language statement ends with dot (full-stop). In programming languages like C, C++ and Java, statement ends with semicolon.
Thus, "int age;" is called statement in C++ and since it declares a variable, it is called declaration statement.
(2)This "age=20;" too is a statement as it ends with semi-colon. This statement assigns value to the variable, hence it is called assignment statement.
(3)This "int age=20;" statement does two job. It declares variable as well as assigns value to it. This is compound statement of both declaration as well as assignment statement.
(4)In C++ (=) is called assignment operator and not equal to as in mathematics. It is use to assign the value. In C++ equality operator is (==).
(5)As already discussed in earlier lesson that cout in C++ is used to displays output to the console. Similarly, cin is used to take the input (a value) from the user and then assign it to its variable.

Check out this stream