Data types in cpp

Data types in cpp : In this tutorial we are going to see different types of data types available in cpp programming.

What is data type

A datatype specifies the type of which variables holds the data.

In C++, their are various types of data type as integer , float ,double,character etc lets see one by one.

Data types in cpp : 

1. Integer

This data type is used to store integer type of data only.

No decimal type of data is allowed.

It takes 2 bytes of memory to store int type of data.

Range of int type :- -32768 to 32767

Example :- int number = 10;

2. Float data type

This data type is used to store floating type of data.

It takes 4 bytes of memory to store float type of data.

Range of float type :- 3.4e-38to 3.4e38

Example :- float number = 10.0;

3. char data type

This data type holds character data.

It only stores single character.

It takes one bytes.

Range of char type :- -128 to 127

Example :- char ch = ‘c’;

Data type modifiers

Different data type has its default behavior as int type stores 2 byte in memory.

To modify default behavior of datatype c++ provides different modifiers which are as follows

1. Short data type

Short keyword is used with int and float data type.

It can not used for char.

short takes 2 bytes.

2. Long data type

Long is used to store more memory to int and float type of data.

Example :- long int x; it will take 4 bytes memory.

In the case of float double keyword is used to store long float.

3. Signed data type

Signed used for storing int and character.

It can’t used with float type.

4. Unsigned data type

Unsigned only used to store positive data.

It can’t used to store float data.

Program for datatype

123456789101112131415161718192021222324252627282930313233#include <iostream>using namespace std;int main(){int no;//declaring int data typefloat no1;//declaring float data typechar ch;//declaring char data typecout<<“Enter int data:”<<endl;cin>>no;cout<<“Enter float data:”<<endl;cin>>no1;cout<<“Enter char data:”<<endl;cin>>ch;cout<<“int data=”<<no;cout<<“\n”;cout<<“float data=”<<no1;cout<<“\n”;cout<<“char data=”<<ch;    return 0;}

Output : 

data types
data types

Program description :

1 .In above program we demonstrate data type in c++.

2. First we declare variable as keyword of data type we want and variable name.

=> int no  = > declaring int data type

float no1;   =>  declaring float data type

char ch;  =>   declaring char data type

3. Next We take input from user (int ,float and char and store it to respective variable).

=> cin>>no;  = 10

cin>>no1;  = 10.0

cin>>ch;  = s