C and C++ -What is data type?

The C++ data types defines how memory are used in a program.We know that all the information in the memory in a Computer is stored in the form of 1 and 0(also known as binary digits), but these 1 and 0 are converted to human readable data or characters. For instance, for English speaking people the binary digits will be converted to English Alphabets, for Japanese people, the binary digits are converted to Japanese Alphabets(Hiragana, Katakana or Kenji) likewise for different people speaking different languages the binary digits are converted to their own native characters. The entity which allows us to convert the binary bits into meaningful and comprehensible characters are known as data types.

There are two major classification of data types in C and C++.They are given below.
 
i)Built-in data type and
ii)User-defined data type.

A brief description of built-in data type is given here.The user-defined data type is discussed in another post.

Link :Some other types:Void , bool, decimal, octal, hexadecimal, etc.


Built-in data type

Built-in data type are those data types which are hardcoded in the compiler and so the compiler can understand them. You cannot change what they represent.

In C++ the built-in data type is inherited from the C language. So whatever is discussed in this chapter and the next, they are common to both the C and C++ languages.

Built-in data type can be divided into seven different types. Each of this type can represent various data in your program.Only brief description of each of them is given here, a thorough description can be found in another post.

i)Integral type:This type can represent integer values in your program. And the keywords 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 keywords included under this type is char.

iv)String:This type can represent a text or a sentence.

v)Wide character:It can represent Unicode characters- 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 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.
 
C C++ data types
 

The seven types 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 meanings and the minimum size they occupy in a memory.

Data type Meaning minimum size
short int or short Can represent short integer
(integer of smaller size)
2 bytes
Int Integer(large integer) 4 bytes
long int long integer(larger integer) 4 bytes
long long they can represent largest integer 8 bytes
Float Can represent real number 4 bytes
double Can represent real number with larger size 8 bytes
long double Can represent real number with largest size 12 bytes
Char Can represent characters 1 byte
wchar_t Can represent unicode characters 2 bytes(in Windows),4 bytes(in Linux)
char16_t Can represent unicode characters 2 bytes
char32_t Can represent unicode characters 4 bytes
bool Can represent truth value:true(1) and false(0) 1 byte
string Can represent a collection of characters(words or sentence or text lines) 4 bytes
wstring Can represent unicode characters string 4 bytes
void Can represent nothing 0
decltype(nullptr) Can represent null pointer

 

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.


C++ Size of different data types

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

#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++.Note the size printed out here is for your particular machine and compiler, another machine may produce different result.