C++ Variables and Data Types

The main feature of the C++ is that you need to define everything before using it. C++ Variables are the names assigned to the memory locations where different type of data is stored. Data types define the type of the data to be stored in the memory locations. The variable are defined in this way

Type Variable;

or 

Type Variable=initialization;

There are seven major types of data which are known as primitive C++ data types.

  1. Integer
  2. Float
  3. char
  4. double
  5. bool
  6. void
  7. wchar_t

These data bypes are defined by keywords which are given below

TypeKeyword
Integerint
Characterchar
Floating point float
Double Floating pointdouble
Booleanbool
Valuelessvoid
Wide characterwchar_t

Integer Data Type:
Integer data types define the variables which can process integers. In fact defining the integer types of variables
define memory locations where only integer types of data can be stored. These are defined by the word “int”.
e.g. int a; int a, x, abc, int FirstNumber, int _seondNm=44;

These data types can be extended by using different modifiers e.g. singed, unsigned, long, short, 
Float Data Type:

Floating type of data is defined by the word “float”. All the fractional numbers eighter signed or signed are defiend by the floating data type. This is single precision data type.
The examples are float x, float y, float z=-5.44;

Double Data Type:

This is double precision data type. it is defined by the keyword double e.g. double xy,

Character Data Type:  

This type of data defines the characters by using keyword char. The example is char  x, myname=’imran kanjoo’;

Boolean Data Type:

The Boolean type of data is defined by the keyword “bool” which hold the Boolean values i.e. true or false. 

Void Type:

These are actually valueless data types. These are defined by the word void.

wchar_t:

This type of data variables are double character wide.

The memory space reserved by these data types are given in the following table

TypeTypical Bit WidthTypical Range
char1byte-128 to 127 or 0 to 255
unsigned char1byte0 to 255
signed char1byte-128 to 127
int4bytes-2147483648 to 2147483647
unsigned int4bytes0 to 4294967295
signed int4bytes-2147483648 to 2147483647
short int2bytes-32768 to 32767
unsigned short int2bytes0 to 65,535
signed short int2bytes-32768 to 32767
long int8bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
signed long int8bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long int8bytes0 to 18,446,744,073,709,551,615
float4bytes+/- 3.4e +/- 38 (~7 digits)
double8bytes+/- 1.7e +/- 308 (~15 digits)
long double8bytes+/- 1.7e +/- 308 (~15 digits)
wchar_t2 or 4 bytes1 wide character

Size of the Variable

The size of the variables depend upon the compiler and the computer or system using and therefore can very. The built in function sizeof can be used to check the sizes of the various data types of the systems in use.

The following example shows how to use the function sizeof.

#include <iostream>
using namespace std;
int main() {
   cout << "Size of int : " << sizeof(int) << endl;
   cout << "Size of short int : " << sizeof(short int) << endl;
   cout << "Size of long int : " << sizeof(long int) << endl;
   cout << "Size of char : " << sizeof(char) << endl;
   cout << "Size of float : " << sizeof(float) << endl;
   cout << "Size of double : " << sizeof(double) << endl;
   cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
   return 0;
}

In the above example the operator << is used to display the output. The endl has been used to end the line.

The output of the above code will be similar to the given below:

typedef Declarations:

There are many situations when we need to give new name to the existing data types. It is just for our clarity and the new names works in the same manner as pre-defined. The syntax to define new name for the existing data type is

typedef type newname;

For example to give new name to the float we can write

typedef float meters;

Enumerated Types:

Enumerated type defines an optional type name and a set of identifiers zero or more. This set can be used as values of the define type. If it has zero identifier then it has no values. To create the enumeration types we use the following general syntax.

enum enum-name { list of optional-names } var-list;

The following example clarifies how to define enums

enum fruit { apple, orange, banana, melon } f;
f = orange;

by default the value of first name in the list is 0 and the value of further names increment by one from the preceding values i.e. the value of apple is 0 and the value of banana is 2. But we can change these values and assign new values as well. For example we need to assign 6 to the banana then we can write as

enum fruit { apple, orange, banana=6, melon } f;

In this case the value of banana will be 6 and the value of melon will be 7.