Variables declaration in C++

Creating a variable reserves a memory location, or a space in memory for storing values. The compiler requires that you provide a data type for each variable you declare. Here some point to remember -

  • C++ offer a rich assortment of built-in as well as user defined data types.

  • Integer, a built-in type, represents a whole number value. Define integer using the keyword int.

  • C++ requires that you specify the type and the identifier for each variable defined.

  • An identifier is a name for a variable,function,class, module, or any other user-defined item. An identifier starts with a letter(A-Z or a-z) or an underscore (_), followed by additional letters, underscore, and digits (0 to 9).
    For example:
    Define a variable called var that can hold integer values as follows:

      int var=10;

Now, let's assign a value to the variable and print it.

using namespace std;
int main()
    int var=10;
    return 0;



Note: The C++ programming language is case-sensitive, so var, Var and VAR are different-different identifiers.

Define all variables with a name and a data type before using them in a program. In cases in which you have multiple variables of the same type, it's possible to define them in one declaration, separation, separating them with commas(,).
For example:

int a,b;
//define two variables of type int

A variable can be assigned a value, and can be used to perform operations.
For example:

int a=30;
int b=15;
int sum =a+b;
// Now sum equals 45
0 Comments 0 Comments
0 Comments 0 Comments