C++ Variables, Identifiers and Data Types

Variables are used for data that can change during program execution. All variables have a name, a type, and a scope. The programmer assigns the names to variables, known as identifiers. An Identifier must be unique within a scope of the C++ program. Variables have a data type that indicates the kind of value they can store. 


Data Types

The data type indicates the attributes of the variable, such as the range of values that can be stored and the operators that can be used to manipulate the variable. C++ has various primitive data types built into the language. We can also create our own data types.

The primitive data types are listed below:

  • Integer:  int (4 bytes), short (2 bytes) and long (4 bytes).
  • Floating-point:  float (4 bytes) and double (8 bytes).
  • Character:  char (1 byte).
  • Boolean:  bool (1 byte).

The following program will produce the accurate size of various data types using the sizeof() operator:

#include <iostream>

using namespace std;

int main() {
//basic data types in C++
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short : " << sizeof(short) << endl;
cout << "Size of long : " << sizeof(long) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of bool : " << sizeof(bool) << endl;
return 0;


Size of int    : 4
Size of short  : 2
Size of long   : 4
Size of float  : 4
Size of double : 8
Size of char : 1 Size of bool : 1


Variable Declaration

When we declare a variable we assign it an identifier and a data type.


type variable = value;

The term “type” can be a data type listed above.

For Example,

int val = 10;

In the above statement, int is the data type for the identifier named val


Identifier Naming Rules

  • It can consist of upper and lower case letters, digits, dollar sign ($) and the underscore (_) character.
  • It must begin with a letter, dollar sign, or an underscore.
  • It is case sensitive.
  • The keywords cannot be used as identifiers.
  • Within a given section of our program or scope, each user defined item must have a unique identifier.
  • It can be of any length.


The typedef declaration

We can create a new name for an existing data type using typedef. Following is the simple syntax to define a new type using typedef −

typedef type newtype; 

For example, the following statement tells the compiler that meter is another name for int −

typedef float meter;

Now, the following variable declaration is legal and creates a variable of type float called height−

meter height;


Enumerated Type

An enumerated type declares an optional type name and a set of zero or more identifiers that can be used as values of the type. Each enumerator is a constant whose type is the enumeration.

Creating an enumeration requires the use of the keyword enum. The general form of an enumeration type is −

enum enum-name { list of names } variable-list; 

Here, the enum-name is the enumeration’s type name. The list of names is comma separated.

For example, the following code defines an enumeration of dimensions called dimension and the variable dm of type dimension. Finally, dm is assigned the value “height”.

enum dimensions { length, breadth, height } dm;
dm = height;

By default, the value of the first name is 0, the second name has the value 1, and the third has the value 2, and so on. But we can give a name, a specific value by adding an initializer. For example, in the following enumeration, breadth will have the value 7.

enum dimensions { length, breadth = 7, height };

Here, height will have a value of 8 because the value of each name will be one greater than the one that precedes it.


Strings in C++

Finally, there’s also a special kind of data type called string which are used for storing text. A string variable contains a collection of characters surrounded by double quotes.

Create a variable named message of type string and assign it a value:

string message = "hello";