Sunday, 19 May 2013

Data Types


What are data type? Data types are those classified keywords which defines the type of variable or the nature of variable.  Although there are many data types but usually there are four primitive built-in datatypes which are regularly used by programmer in a programming language namely:
  1. Integer         (int)
  2. Float           (float)
  3. Double        (double)
  4. Characters  (char)
While doing programming in any programming language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
You may like to store information of various data type like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Along with the datatypes modifiers can be prefixed as per the requirement of the program. You must be  wondering what are these modifiers. Modifiers are those keywords which precedes the data types. Some of the basic modifiers are unsigned, signed, long, short, etc. These may or may not be compatible with some of the datatypes.
These are some issues regarding the incompatibility of the modifiers with some datatypes like long and short modifiers can not be used with char datatype. So here are some key points which beginner or a programmer must always remember:
  • long and short modify the maximum and minimum values that a data type will hold.
  • A plain int must have a minimum size of short.
  • Size hierarchy : short int < int < long int
  • Size hierarchy for floating point numbers is : float < double < long double
  • long float is not a legal type and there are no short floating point numbers.
  • Signed types include both positive and negative numbers and is a default type.
  • Unsigned, numbers are always without any sign, that is always positive.
Here is the list of all the possible data types which are used by a programmer (rookie/expert/professional ) on a regular basis.

Datatype
Modifier(Signed/unsigned) 
Modifier(Long/Short)
Basic Data Type
size of data type
 int
Default
Default
int
4
 long
Default(signed)
long
Default(int)
4
 short
Default(signed)
short
Default(int)
2
 signed
signed
Default
Default(int)
4
 unsigned
unsigned
Default
Default(int)
4
 signed short
signed
short
Default(int)
2
 unsigned short
unsigned
short
Default(int)
2
 signed long
signed
long
Default(int)
4
 unsigned long
unsigned
long
Default(int)
4
 long signed int
signed
long
int
4
 long unsigned int
unsigned
long
int
4
 short signed int 
signed
short
int
2
 short unsigned int
unsigned
short
int
2
Short int
Default(signed)
short
int
2
long int
Default(signed)
long
int
4
long long 
Default(signed)
long long
int
8
long long int
Default(signed)
long long
int
8
long long signed int
Default(signed)
long long
int
8
long long unsigned int
Default(signed)
long long
int
8
long long signed
Default(signed)
long long
int
8
long long unsigned
Default(signed)
long long
int
8
 signed int
signed
Default
int
4
 unsigned int
unsigned
Default
int
4
 char
Default(signed)
Default
char
1
 signed char
signed
Default
char
1
 unsigned char
unsigned
Default
char
1
 float
Default
Default
float
4
 long float
Default
long
float
8
 double
Default
Default
double
8
 long double
Default
long
double
8
 bool
Default
Default
Default
1
 int*
Default
Default
 int*
4
 char*
Default
Default
char*
4
 float*
Default
Default
float*
4
 double*
Default
Default
double*
4
 wchar_t
Default
Default
 wchar_t
2
 short *
Default
Default
int *
4
 long* 
Default
Default
int *
4
 signed*
Default(signed)
Default
int *
4
unsigned *
unsigned
Default
int *
4










The above table shows the variable type, modifiers and there cases of implementing with datatypes, Basic data types and how much memory it takes to store the value memory. 
The size of the above given data types may vary from machine to machine. So to see the correct size of various data type on your computer the following program coded in C++ may try help you to get an idea of the size of your datatype used by your computer:

#include <iostream>
using namespace std;

int main()
{
   cout << "Size of char : " << sizeof(char) << endl;
   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 float : " << sizeof(float) << endl;
   cout << "Size of double : " << sizeof(double) << endl;
   cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
   return 0;
}

This example uses endl which inserts a new-line character after every line and << operator is being used to pass multiple values out to the screen. We are also using sizeof() function to get size of various data types.
When the above code is compiled and executed, it produces following result which can vary from machine to machine:

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

No comments:

Post a Comment