# Variable and Data Types in C++

When we create a program, we need to store some data. This data can be further manipulated and used for different purposes. To store this data, we need to know

• Value of the data
• Type of data

For all these points, we use variables. Take for instance

var k = 25;


Here k is the variable, 25 is the value of k, k is an integer type. It does not tell the address of the data. For getting the address we can use the & operator (will be described later in detail). Whenever we store the data we use variable, so we should know some rules to name the variables.

## Rules to name variables

• Variables can only have alphabets, numeric digits, and an underscore (_) charater.
• You can not use a keywords like main, int, float, etc.
• The first character can not be a digit
• C++ is case-sensitive, so uppercase characters are different from lowercase characters.

It is always a good practice to follow the rules. Besides that, you should name the variables so that you can easily understand the value it holds. Example of good names can be –

• studentsName
• messFees etc.

The same can be written as

• Studentsname
• messfees

But it creates a problem in reading the code. So casing is one of the ways, you can write the neat code. Else, you can use the underscore(_) for getting more clarity, for instance

• students_name
• mess_fees

But you should keep in mind that all the variable defined here will be different from each other, i.e. myAddress will be different from my_address, until both are made equal on purpose.

Now, that you know how to name a variable, you should also know the types of variables you can create. All of these variables use data-type to store the particular type of data.

## Categories of Data types

Data types in C++ are divided into two categories – primitive data types and abstract data types.

Primitive data types are built in data types, and these can be used directly. While,abstract data types are user defined data types and these are defined by the user.It is like defining a class. In this section, we will see only the primitive data types.

There are different fundamental data-types. These are

• Six different integer types
• Three character types
• Boolean type
• Three floating types.

Let’s discuss each of the following types in detail.

## Integer Types

There are six integer types in C++. These are short, int, long, unsigned short, unsigned int and unsigned long. The same are also the keyword used to define the particular type. For instance,

int a;
short b;
long c;
unsigned short d;
unsigned int e;
unsigned long f;


Different type of integers are used as per the memory available. The problem is mainly in embedded system or high applications. You can use any of the type depending on the usage and the range and size of the data-type. You can see the range and size of these data types in this article. We can use function sizeof() to get the size of the data type. In general, short is 2 bytes, int is 4 bytes and long is 8 bytes. It remains same for the unsigned data type.

## Boolean Type

Boolean data types are used to store the boolean values. It can only store true or false. These values can also be stored as a 0 or 1. bool is used as the keyword for the boolean values. Take for instance,

bool k = false;
bool p = 1;


Here k stores the value of false and p is true, as the value for p is 1. Value 0 in a boolean variable is always treated as false, rest all other are considered true.

## Character Type

Characters type is used for variables to store and represent characters. These can be letters, digits or some signs. char is the keyword used to form the variables. It requires 1 byte of memory. Take for instance,

char a = ‘A’;
char b = ‘5’;


Character type is an integral type, it can represent letters, digits, punctuation marks etc. The three different data types are char, unsigned char and wchar_t.

## Floating Point Type

The term floating point is used for the numbers with fractional values. It is the scientific notation that is used for representing a number. For instance

float a = 6.5;
float b = 2.3x10^3


There are three different types of floating-point types. These are float, long and long double. float uses 4 bytes, double uses 8 bytes and long double uses 8 or more bytes. It can be found in the following article. Float integer types are stores in the form of mantissa and exponent. The number is converted into binary form. It is written in the form of binary and in the power of 2.

The binary form is the mantissa and the power of 2 is exponent. Keywords used for floating point type are float, long, and long double.  Whenever some operations are done on float and integer, the integer is converted to float and then the operations are performed. All the float operations are performed in the double precision.

All of these data types are used for a particular purpose. If you need a different data-type, you can always define your own data-type using classes, which will be discussed later.