Explain Primary Data Types With Examples
Get Our Latest Interview Questions: Aptitude, Reasoning, English, GD, HR-PI
Primary Data Type:
Primary Data Type is divided into 4 Parts:
1. Int
2.Char
3.Float
4. Double
1. Int or integer:
Int is use to store whole numbers, which has no fraction value or no decimal point.
It Occupies 2 byte of memory.
And to declare data into integer, int keyword is used.
When we are taking value from keyboard we are using %d sign in integer.
Int Ex:
void main()
{
int a,b,c;
a=2,b=3,c=3.5; // Deceleration of Integer Type Variable
}
Here the value of a is 2, b is 3, c is 3.5. in this given value whole number is 2 & 3, where 3.5 is fractional value so a and b is Wright and c is wrong.
2. Char or character:
Char is used to store characters.
It only takes alphabets and special symbols enclosed in single quotes, like = aman, rahul, ‘ab’,’cde’,’abha’ etc.
It occupies 1 byte of memory.
To declare character we use char keyword.
When we are taking value from keyboard we are using %c sign in integer.
Char Ex:
void main()
{
Char a,b,mohan; a=‘aman’, b=‘book’; // Deceleration of char type variable
}
Here we declare 3 variable a,b and mohan is character, its Wright. For every char variable it occupies 1 byte in memory.
3. Float:
Float is used to store the decimal type values as well as we can also store whole or integer values.
It occupies 4 byte in memory.
To declare any variable as a float we use float keyword.
Float is having some limitations, after point (.) it takes only 3 to 4 place values, so for long decimal point values we can’t use float data type, for that we need to use double data type (Read Next).
When we are taking value from keyboard we are using %f sign in integer.
Float Ex:
void main()
{
Float a,b,c;
a=2.333, b=2121.2, c =12;
}
Here a & b contains decimal value but c contains whole number, so it is Wright because float contains fractional value as well as whole numbers.
4. Double:
This is also used to store float or decimal values.
The only difference is float is having limitation in taking after point values but in double there is no limitation.
It occupy 8 bit memory, and for storing 8 byte memory is used.
When we are taking value from keyboard we are using %lf sign in integer.
Double Ex:
void main()
{
double a,b,c; a= 2, b =2.323,c =212.32232323;
}
Here a,b and c contains different - different values. But all 3 are Wright, because double also contains int type value and float type values.
Expert Review: Here we declare all data types with example, so you can learn easily. this is very important as on Interview point of view. Many HR Asked: Write a program to demonstrate data type.