Start a Project
Varibles in C

Keywords & Identifiers:

Keywords in a programming language are also called reserved words. These are words that the language has reserved for itself and are used for specific purposes. A programmer cannot redefine them or use them as variables.
For example: int, float, if, return, class, etc.

An identifier is the name you use for a variable or any object in C++. An identifier is a string consisting of alphanumeric characters (letters and numbers). There are a total of 53 characters from which an identifier can start. From these 53 characters, 52 are alphabetic characters (26 upper case letter and 26 small case letter ) and one is the character underscore ( _ ). The first character of an identifier can be an underscore ( _ ) but this is highly discouraged. It is recommended that the first character be a letter. Additionally, there are 10 numeric characters. But, the first character of a identifier also cannot be a number or a C++ operator (like +, -, etc).

Since, C++ is a case-sensitive language, uppercase and lowercase letters are treated differently. For example, sum and SUM are considered different identifiers.

Variables in C++

A variable is a container (storage area) used to store data in a computer’s memory. The name of a variable is actually the name of the memory segment that you use to store values in a C++ program. Each memory segment you define in your program can store a specific type of data, meaning that the type of the variable determines what kind of data it can store.

Note: What is a “type”? To Learn about types click here.

For example, if you declare a variable to store a number, then you cannot store a character or a decimal value in it. You can only store numerical values like 1,2,3 etc. This is how you can declare variables.

int number; 

You can also declare multiple variables at once:

int number1, number2;

The next step is variable initialization (assigning a value to a variable). The information you store in variable or in other words in memory is called the value of the variable.

You can initialize a variable at the time of declaration like this:

int number = 81;

Alternatively, you can declare a variable and then initialize it on the next line.

int number1, number2;
number1 = 10;
number2 = 101;

Or using a long data type:

long sum;
sum = number1 + number2;
sum = 10131;

Let us understand variables through an example:

// This program shows initialization of variables.
#include 
using namespace std; 
int main()
{
     // Declaration
    int a;
    short b;
    long sum;

    // Initialization
    a = 10;
    b = 7;

    // Summation
    sum = a + b;

    cout << "The Sum of "<< a << " and " << b 
         << " is " << sum;
         return 0;
}

Output:

The Sum of 10 and 7 is 17

In this program:

The program outputs the sum of variables a and b. The type of each variables is:

a (type int)

b (type short)

sum (type long)

Values are assigned to a and b, meaning they are initialized. The sum of a and b is stored in the sum variable. The program outputs the sum.


Leave a Comment

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