Data Types

A variable must have a data type like integer, decimal numbers, characters etc. The variable of type Integer stores integers and a character type variable stores characters. The primary difference between 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.
  • int
  • short
  • 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) 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.
Sample Program 1
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.


The first three lines are the same, we have discussed already in our first program. Now as we need to use variables in our program. So we first declare three variables x, y and z with their data type integer 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


short Data Type:

We noted that the integer has four bytes in memory. So if we have to store a small integer like 5, 10 or 20 it will occupy four bytes. 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.



long Data Type:
stored in 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.


Here is the output of the above program.


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:

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’.


The output will be as:

For previous lesson click here: Assignment Operator
For next lesson click here: Arithmetic Operators


the easiest way to learn programming
introduction to programming
Data Types

0 comments:

Post a Comment

 

introduction to programming Copyright © 2011-2012 | Powered by Blogger