Home > C++ > Data Types in C++

A data type is the classification of data i.e. telling the compiler that programmer is using this data. Programming languages support different data types.

C++ data types

According to Microsoft these Different Data Types in C++ are divided into three categories integral, floating point, and void.
Integral types are capable of handling/storing whole numbers e.g. 1, 898 etc. while Floating point types are capable of specifying values that may have fractional parts e.g. 3.6,11.80 etc.
And third type void is used to describe empty values. This type is used with functions declaration i.e. with those functions that do not return any value.
E.g.

void myFunction()
{
	// function body;
}

Note: – You can’t use void to declare a variable.

Integral types

Data type int: –

int is the integer data type. Microsoft C/C++ supports four integer types according to their sizes. i.e. 8, 16, 32 & 64 bits. you can declare these types of integers as

    • __int8 v1; // Declares 8-bit integer synonym to ANSI char
    • __int16 v2; // Declares 16-bit integer synonym to ANSI short
    • __int32 v3; // Declares 32-bit integer synonym to ANSI int
    • __int64 v4; // Declares 64-bit integer no synonym in ANSI

E.g.

#include "stdafx.h"
#include "iostream"

using namespace std;
void main()
{
	__int8  v1 =0;       // Declares 8-bit integer   
	__int16 v2=10;     // Declares 16-bit integer   
	__int32 v3=20;     // Declares 32-bit integer 
	__int64 v4=30;      // Declares 64-bit integer 

	cout << "8 bits Integer : " << unsigned(v1) << endl;
	cout << "16 bits Integer : " << v2 << endl;
	cout << "32 bits Integer : " << v3 << endl;
	cout << "64 bits Integer : " << v4 << endl;

getchar(); // To hold the output console screen.
}

Output: –

int data type - integers

Data type char: –

If you want to store alphanumeric and non-alphanumeric characters you can use char data type. In Microsoft C++ by default, char data type stores ASCII values for characters in it.
Different variations in this data type are also available.
char, wchar_t, char16_t, char32_t
char is 8 bits in size.
wchar_t, char16_t are 16 bits in size.
char32_t is 32 bits in size.
E.g.

#include "stdafx.h"
#include "iostream"
using namespace std;
void main()
{ 
	// 8 bits character
	char ch1 = 'a'; 

	//16 bits character
	char16_t ch2 = u'—Ď';
        wchar_t  ch3 = L'c'; 

	//32 bits character
	char32_t ch4 = 'd'; 

	cout << "8 bits character : " << ch1 << endl;
	cout << "16 bits character : " << ch2 << endl;
	wcout << "16 bits character : " << ch3 << endl;
	cout << "32 bits character : " << ch4 << endl;

} 

Output: –

Different Data Types in C++ - characters

Data type Bool: –

You can save Boolean values (true or false) in Bool data type. It can only have one of the two values i.e. either true or false.

Other data types included in Integral type are short and long.
Floating point type contains:

Float, Double & long Double all these types are used to store values that may contain fractional parts.

Data TypeStoreSize
intInteger(4bytes -2147483648 to 2147483647)
floatFloating point(4bytes +/- 3.4e +/- 38 (~7 digits))
boolBoolean(1byte True or False)
charCharacter(1byte -128 to 127 or 0 to 255)
stringStrings(28 bytes)
user defineduser defined(depending how they are created)

Leave a Reply

Your email address will not be published. Required fields are marked *