Data type and their type

In this page we will discuss,

 

Data type

as we know, to store any information or value in any program, we first declare the variable. The type of variable that comes from the type of value that it stores will reserve the space in memory for itself., which means that the value stored in the variable will be any digits or a character.

In simple language, the meaning of the data type is from the data/information entered by the user, which means data entered by the user is numeric or character or decimal. Different keywords are used in C ++ to store all these data.

Type of Data type

There are three type of Data Type in C++

  • Built in data type
  • Derived data type
  • User defined data type

You can understand this from the following diagram

 

Built in data type

intit will used to store only numeric values

floatit used to store only decimal values.

charused to store only character type values.

void

It determines whether a function is a return type or not, it is used when a function does not return any type of value (exception constructor and destructor).

If there are no parameters or arguments in any function, then instead of parameters, void is used (inside the bracket).

It is used to hold the address of different type of data in the same pointer-variable.

These can be easily understood with the help given below.

Example: –

In the program below, you can see that to store digits, float to store decimal value, and char data type to store a single character-

#include<iostream.h>
#include<conio.h>

void main()
{
  int i=10; // store digits type value
  float f=4.5; // store only float type value
  char c='a'; // store only single character
  char name[]="Rahul"; // for String value

  clrscr();
  cout<<"Digit type value : "<<i<<endl;
  cout<<"Decimal type value : "<<f<<endl;
  cout<<"character type value: "<<c;
  cout<<"String type value : "<<name;
 
 getch();
}
OUTPUT:
Digit type value: 10;
Decimal type value: 4.5
character value: a
String type value: Rahul

Remember, a variable space in the memory depends on their data types.

Built-in data types take space in memory as follows-

NAME size (in byte) Range
int 2 -032768 – 32767
char 1 -128 – 127
float 2 3.4E – 38 to 3.4E+38
double 8 1.7E – 308 to 1.7E+308

 

You can understand this with the help of the program given below, where using sizeof() you can know the memory size of a data type or variable, that is, how much size is taking in a variable memory.

To know data type size in own system using sizeof operator

#include<iostream.h>
#include<conio.h>

void main()
{

 int age=25,x;
 char y ='c';
 char name[]= "Rahul"; //{'R','a','h','u','l','\0'};

 clrscr();
 cout<<"size of x variable : "<<sizeof(x)<<" ";
 cout<<"\nsize of int data type: "<<sizeof(int)<<" ";
 cout<<"\nsize of age variable : "<<sizeof(age)<<" ";
 cout<<"\nsize of y variable : "<<sizeof(y)<<" ";
 cout<<"\nsize of char variable: "<<sizeof(name);
 getch();
}
OUTPUT:-
size of x variable : 2 
size of int data type: 2 
size of age variable : 2 
size of y variable : 1 
size of char variable: 6

As you can see that int type variable take 2 byte, float type variable take 2 byte, char type variable take 1 byte, and name variable (which is the character of sequence) take 6 byte (for each character by 1 byte and NULL character Even for 1 byte) in the memory.

A data-type depends on the memory size compiler. Like the 16 bit compiler, it will take int 2 byte while the 32 bit compiler can reserve memory by int 4 byte. So it can be different in any system.

Where we can store only one type of value in it, while storing different type of information we can use other data type e.g.derived and user-defined, derived data type and user defined data type which are defined by the programmer, we will discuss about them further.

things to know

char data type stores all types of values ​​(numeric, social character, decimal type character type) whereas int and float data type can only store numeric type data.

Here, storing a data means data behavior, that is, if we store a numeric value in the char data type, then it will easily store that numeric value, but we can not used these data for the mathematical task. It means that it will behave like a character.

In addition, int and float data types are sensitive, they will only store numeric values ​​if we store different type values ​​in them, then the program execution will return an error or the program runs in a different flow.

In a large program, such a situation may arise that we may have to store a data in a different data type, so some system defined function has been provided in C++. You can read them in the same tutorial (Previously it has been given a small introduction in the page structure of a program in c ++)

as well as these functions have been used in the given program/project.

 

Related Exercise

previous-typedef Data-Type in C++

next-Control Statement and their type