Data type is classification of a particular type of data like integer, float, character, boolean etc. It is mainly classified in two types primary and seconday data types as shown in figure.
Primary Data Types
- Can store only single signed or unsigned character.
- Both occupy 8 bit storage space, but their ranges are different.
- For unsigned character its value range from 0 to 255 and for signed characters its from -128 to 127.
- Declaration and initializing syntax: char <variable name>; and char <variable name> = '<single character>';
- Eg: char value;
char value = 'a';
- Can store only whole numbers and fractional numbers are not allowed.
- Divided into three classes short int, int and long int. And all these data types have two forms signed and unsigned.
- Each data type memory size and ranges are different.
- Declaration and initializing syntax: int <variable name>; and int <variable name> = <whole number>;
- Eg: int a;
int a = 2;
short int b = 3;
long int c = 4;
- Can store real numbers with 6 digits precision.
- Requires 4 byte of memory space and ranges from 3.4Eľ38 to 3.4E+38
- Declaration and initializing syntax: float <variable name>; and float <variable name> = <real number>;
- Eg: float percentage;
float percentage = 49.21;
- When precision of float data type is short, we go for double which can store larger value and precision.
- Double requires 8 bytes and long doubel requires 10 bytes.
- Declaration and initializing syntax: double <variable name>; and double <variable name> = <real number>;
- Eg: double speed_rate;
double speed_rate = 1349.213235644;
- Void specifies it has no value.
- Mainly used in functions to speicify it does not return any value from it.
- Eg: void main()
Secondary Data Types
Following data types are disccused in upcoming points.
- Enum etc.
Choosing proper data type
Data types like long integer, double, long double are slower in arithmetic operations as they occupy more memory, making the program larger. So you should use smallest variable data type that is suitable for your program.