what are data types in C and C++ : data types and it’s sizes.


data types in C and C++ and their sizes

Data types in C and C++ defines how memory are used in a program. Using data types we can store information/data in the memory.In every program we will make use of English characters , numbers(integers and real numbers ) and some other special symbols like |,!,#,&,*,etc.These characters , numbers and symbols are used and stored using data types and variables.English alphabet are found in the English keyboard but,what about Hindi or Japanese Hiragana or Russian alphabet which are not found in the keyboard.To make the computer enable the use of all kinds of letter which are found and not found in the specific language keyboard data type is classified into different types.There can be however a two major classification of data types in C and C++.They are given below.

i)Built-in data types and
ii)User-defined data types.
 

In Chapter 1 we will discuss the built-in data types.





 

Built-in data types

Built-in data types are those data types which are hard coded in the compiler and so the compiler can understand them.You cannot change what they represent.They are inherited from the C language so,if you are familiar with C you already know what built-in data type of C++ is.But,those who does not know C,worry not! we will discuss them in detail in this chapter.

Built-in data types can be divided into seven different types.Each of this type can represent various data in your program.How to use these type is not shown here,you can visit the specific post of each type to learn how to use them in your program.The seven types and the keyword(visit this link to know what is keyword Some terms:Variables , Identifier , Keyword , Operators , Operands,literals.) used to implement them is shown below.

i)Integral types:This type can represent integers value in your program.And the keyword included under this type are :int,long int and long long int.

ii)Floating point type:This type can represent real numbers,meaning numbers with decimal point.The keyword included are float ,double and long double.

iii)Character type:This type can represent characters -a singe letter,and the keyword included under this type is char.

iv)String:This type can represent a text or a sentence.The keyword used is string.

v)Wide character:It can represent a Unicode characters-a letters not belonging to English Alphabets,so Japanese letters or Hindi or Russian alphabets,etc. are included under this type.The keyword use is wchar_t.

vi)Wide string:It can represent a Unicode strings ,for instance a Russian word or Hindi sentence,etc.The keyword use is wstring.

vii)Bool:It can represent only two values ‘True’ or ‘false’ and true is denoted by 1 and false by 0.

The seven types given here can be sub-divided into 12 types .Note all the seven types will occupy different sizes and the size they occupy depends on the machine and compiler you are using,the table below shows the different sub-data types with their meaning and the minimum size they occupy in memory.
 

Data typeMeaningminimum size
short int or shortCan represent short integer
(integer of smaller size)
2 bytes
IntInteger(large integer)4 bytes
long intlong integer(larger integer)4 bytes
long longthey can represent largest integer8 bytes
FloatCan represent real number4 bytes
doubleCan represent real number with larger size8 bytes
long doubleCan represent real number with largest size12 bytes
CharCan represent characters1 bytes
wchar_tCan represent unicode characters2 bytes(in Windows),4 bytes(in Linux)
char16_tCan represent unicode characters2 bytes
char32_tCan represent unicode characters4 bytes
boolCan represent truth value:true and false1 byte
stringCan represent a collection of characters(words or sentence or text lines)4 bytes
wstringCan represent unicode characters string4 bytes

 
The integer types:short,int,long int,long long and the char type can be further sub-divided into two types using a specifier known as signed and unsigned keyword.More about these types and their differences will be discussed in detail in another post.
 


 


Earn money($$$) from your website-Join Now!

Size of different data types

Size is one of the properties of any data types in C and C++ and it plays a significant role for any data type while allocating a storage in the memory.Greater is the size of a data type larger is the memory it can allocate and larger is the range of values that type can represent.The program below uses the sizeof( ) operator to obtain the size of all the data types in byte.

*Note::If you have trouble understanding the program below then check out The First Program ,first and then come back.

 
#include <iostream>

using namespace std ;

int main( )
{
cout<< “Size of short int ” << sizeof(short) << endl ;
cout<< “Size of int ” << sizeof(int) << endl ;
cout<< “Size of long int ” << sizeof(long int) << endl ;
cout<< “Size of long long ” << sizeof(long long) << endl ;

cout<< “Size of char ” << sizeof(char) << endl ;

cout<< “Size of wchar_t ” << sizeof(wchar_t) << endl ;
cout<< “Size of char16_t ” << sizeof(char16_t) << endl ;
cout<< “Size of char32_t ” << sizeof(char32_t) << endl ;

cout<< “Size of float ” << sizeof(float) << endl ;
cout<< “Size of double ” << sizeof(double) << endl ;
cout<< “Size of long double ” << sizeof(long double) << endl ;

cout<< “Size of bool ” << sizeof(bool) << endl ;

cout<< “Size of string ” << sizeof(string) << endl ;
cout<< “Size of wstring ” << sizeof(wstring) << endl ;

cin.get() ;
return 0 ;
}
 

You can run the above program and see for yourself the size of different data types in C and C++.Note the size printed out here is for your particular machine and compiler,another machine can give different values.

 


Related Link

->Integral type:Built-in Data types.
 
->Floating point type : Built-in data type.
 
->Char type : Built-in data type.
 
->C++ String type and it’s differences with char type
 
->C++ wchar_t and wstring