Datatype and Literals

WHAT IS DATATYPE ?

Data type means what type of data programmer is using to represent the information. There are associated operations for handling the data.

 

There are Two Datatypes

  1. Fundamental Datatypes  => The datatypes which are atomic i.e. not composed of other datatypes or are undivided.

  2. Derived Datatypes => The datatypes which are made from fundamental datatypes.

There are Five Fundamental Datatypes:

1. int => for integers. Integers are whole numbers.They have no fractional parts.

2. char => for characters. Characters are stored in memory in ASCII code (letters and symbols are represented by thier associated number codes , because of which char datatype are also called another integer type.)

3. float => for floating point numbers(numbers having fractional part.)

4. double => for handling floating point numbers with double precision.It occupies twice the memory of float datatype.

5. void => void type specifies empty set of values. It is used as the return type in functions that do not return any value.No object of void type can be declared.

 

DATATYPE MODIFIERS

Modifiers are used to represent the information more precisely in various situations.

 

INTEGER TYPE MODIFIERS

 

 TYPE  APPROX. SIZE  MINIMAL RANGE
 short  2  -32768 to 32767
 unsigned short  2  0 to 65535
 signed short  2  same as short
 int  2  -32768 to 32767
 unsigned int  2  0 to 65535
 signed int  2  same as int
 long  4  -2147483648 to 2147483647
 unsigned long  4  0 to 4294967295
 signed long  4  same as long

 

short , int and long datatypes are signed by default.

 

CHARACTER DATATYPE MODIFIER

 

TYPE APPROX. SIZE MINIMAL RANGE
char 1 -128 to 127
unsigned char 1 0 to 255
signed char 1 same as char

 

FLOATING TYPE MODIFIERS

TYPE APPROX. SIZE DIGITS OF PRECISION
float 4 7
double 8 15
long double 10 19

 

DERIVED DATATYPES

  1. ARRAYS

  2. FUNCTIONS

  3. POINTERS

  4. REFERENCES

 

5. CONSTANTS

  • "const" keyword is used to declare the constants.

  • value of constant cannot be changed during the program.

  • constant declaration :   const dataTypeName = value;

  • eg : const float = 3.44f;

  • constant must be initalised at the time of declaration.

  • If const is written in place of const int , it means same.

 

USER DEFINED DATATYPES

  1. CLASS

  2. STRUCTURE

  3. UNION

  4. ENUMERATION

WHY SO MANY DATATYPES?

To allow programmer to take advantage of hardware characteristics.

Machines have different memory requirements and computation speeds. Suppose program that earlier was working with 2 byte int , on shifting to machine that provides 4 byte int and 2 byte short can easily work with short of new machine thereby not increasing the memory requirements of program we use different datatypes.