By 25/02/2014 14 Comments

Data Types in C++

A variable must have a data type associated with it, for example it can have data types like integer, decimal numbers, characters etc. The variable of type Integer stores integer values and a character type variable stores character value. The primary difference between various data types is their size in memory. Different data types have different size in memory depending on the machine and compilers. These also affect the way they are displayed. The ‘cout’ knows how to display a digit and a character. There are few data types in C++ language. These data types are reserved words of C++ language. The reserve words can not be used as a variable name. Let’s take a look into different data types that the C++ language provides us to deal with whole numbers, real numbers and character data.

Whole Numbers

The C++ language provides three data types to handle whole numbers.

  1. int
  2. short
  3. long

int Data Type

The data type int is used to store whole numbers (integers). The integer type has a space of 4 bytes (32 bits for windows operating system) in memory. And it is mentioned as ‘int’ which is a reserved word of C++, so we can not use it as a variable name. In programming before using any variable name we have to declare that variable with its data type. If we are using an integer variable named as ‘i’, we have to declare it as int i ;

The above line is known as declaration statement. When we declare a variable in this way, it reserves some space in memory depending on the size of data type and labels it with the variable name. The declaration statement int i ; reserves 4 bytes of memory and labels it as ‘i’. This happens at the execution time.

Example Program # 01

Let’s consider a simple example to explain int data type. In this example we take two integers, add them and display the answer on the screen. The code of the program is written below.

#include <iostream.h>
main()
{
int x;
int y;
int z;
x = 5;
y = 10;
z = x + y;
cout << "x = ";
cout << x;
cout << " y=";
cout << y;
cout << " z = x + y = ";
cout << z;
}

The first three lines declare three variables x, y and z as following.
int x;
int y;
int z;
These three declarations can also be written on one line. C++ provides us the comma separator (,). The above three lines can be written in a single line as below
int x, y, z;
As we know that semicolon (;) indicates the end of the statement. So we can write many statements on a single line. In this way we can also write the above declarations in the following form
int x; int y; int z;
For good programming practice, write a single statement on a single line. Now we assign values to variables x and y by using assignment operator. The lines x = 5; and y = 10 assign the values 5 and 10 to the variables x and y, respectively. These statements put the values 5 and 10 to the memory locations labeled as x and y. The next statement z = x + y; evaluates the expression on right hand side. It takes values stored in variables x and y (which are 5 and 10 respectively), adds them and by using the assignment operator (=), puts the value of the result, which is 15 in this case, to the memory location labeled as z.

Here a thing to be noted is that the values of x and y remains the same after this operation. In arithmetic operations the values of variables used in expression on the right hand side are not affected. They remain the same. But a statement like x = x + 1; is an exceptional case. In this case the value of x is changed.

The next line cout << “ x = “ ; is simple it just displays ‘ x = ‘ on the screen.

Now we want to display the value of x after ‘x =’. For this we write the statement

cout << x ;

Here comes the affect of data type on cout. The previous statement cout << “x = “ ; has a character string after << sign and cout simply displays the string. In the statement cout << x; there is a variable name x. Now cout will not display ‘x’ but the value of x. The cout interprets that x is a variable of integer type, it goes to the location x in the memory and takes its value and displays it in integer form, on the screen. The next line cout<< ”y =”; displays ‘ y = ‘ on the screen. And line cout << y; displays the value of y on the screen. Thus we see that when we write something in quotation marks it is displayed as it is but when we use a variable name it displays the value of the variable not name of the variable. The next two lines cout << “z = x + y = ”; and cout << z; are written to display ‘z = x + y = ’ and the value of z that is 15. Now when we execute the program after compiling, we get the following output.

x = 5 y = 10 z = x + y = 15

short Data type

We noted that the integer occupies four bytes in memory. So if we have to store a small integer like 5, 10 or 20 four bytes would be used. The C++ provides another data type for storing small whole numbers which is called short. The size of short is two bytes and it can store numbers in range of -32768 to 32767. So if we are going to use a variable for which we know that it will not increase from 32767, for example the age of different people, then we use the data type short for age. We can write the above sample program by using short instead of int.

Example Program # 02

#include <iostream.h>
main()
{
short x;
short y;
short z;	
x = 5;
y = 10;
z = x + y;
cout << "x = ";
cout << x;
cout << " y=";
cout << y;
cout << " z = x + y = ";
cout << z;
}

long Data Type

On the other side if we have a very large whole number that can not be stored in an int then we use the data type long provided by C++. So when we are going to deal with very big whole numbers in our program, we use long data type. We use it in program as: long x = 300500200;

Real Numbers

The C++ language provides two data types to deal with real numbers (numbers with decimal points e.g. 1.35, 735.251). The real numbers are also known as floating point numbers.

  • float
  • double

 

float Data Type

To store real numbers, float data type is used. The float data type uses four bytes to store a real number. Here is program that uses float data types.

Example Program # 03

#include <iostream.h>
main()
{
float x;
float y;
float z;
x = 12.35;
y = 25.57;
z = x + y;
cout << " x = ";
cout << x;
cout << " y = ";
cout << y;
cout << " z = x + y = ";
cout << z;
}

 

double Data Type

If we need to store a large real number which cannot be store in four bytes, then we use double data type. Normally the size of double is twice the size of float. In program we use it as: double x = 345624.769123;

char Data Type

So far we have been looking on data types to store numbers, In programming we do need to store characters like a,b,c etc. For storing the character data C++ language provides char data type. By using char data type we can store characters in variables. While assigning a character value to a char type variable single quotes are used around the character as ‘a’.

Example Program # 04

#include <iostream.h>
main()
{
char x;
x = 'a';
cout << "The character value in x = ";
cout << x;
}





Next Tutorial →


← Previous Tutorial


Posted in: C++ Basics

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .