In this C++ tutorial, you will learn about datatypes, different types of datatypes: primitive, derived, and user-defined; datatype modifiers, and how to use datatypes, with examples.

C++ Datatypes

Datatype specifies the amount of memory the value needs, and the characteristics of the value stored in that memory.

For example, a character datatype takes 1 byte of storage and it represents a single character. The character could be an alphabet, white space, digit, special symbol, etc.

Similarly, an integer datatype takes 4 bytes of storage and can accommodate a value ranging from -2147483648 to 2147483647.

Types of Datatypes

There are three categories of datatypes in C++ based on whether the datatype is pre-defined in C++, if the datatype is derived from pre-defined types or a collection of them, or if it is user defined.

  • Primitive Datatypes
  • Derived Datatypes
  • User-defined Datatypes
ADVERTISEMENT

Datatype Modifiers

The storage and range of values a datatype allows can be modified using datatype modifiers.

Using modifier for a datatype is optional.

There are four datatype modifiers in C++. They are

  • signed – Leading bit is used for storing sign of the value.
  • unsigned – No sign bit allocated for value. All the storage is used for magnitude of value.
  • short
  • long

Based on the specific datatype, all or some or none of these modifiers could be applied to a specific datatype.

We shall look into the datatypes and the modifiers that could be applied to these datatypes.

Primitive Datatypes

The following table describes the datatypes and the different variations of those datatypes, if any.

Datatype NameC++ KeywordApplicable Modifiers
Integerintsigned, unsigned, short, long, signed short, unsigned short, signed long, unsigned long, long long, unsigned long long
Charactercharunsigned, signed
Booleanbool
Floating Pointfloat
Double Floating Pointdoublelong
Valueless / Nothingvoid
Wide Characterwchar_t

The following code snippet shows how to declare variables of different primitive datatypes with applicable modifiers.

int x1;
unsigned int x2;
signed int x3;
short int x4;
long int x5;
signed short int x6;
unsigned short int x7;
signed long int x8;
unsigned long int x9;
long long x_1;
unsigned long long x_2;

char ch;
unsigned char ch1;
signed char ch2;

bool b;

float f;
double d;
wchar_t w;

The size of storage allocated to a specific datatype varies from compiler to compiler. But, we could programmatically find the size allocated to a specific datatype using sizeof() method.

Example

In the following program, we will find the number of bytes allocated to different primitive datatypes along with their applicable modifiers.

C++ Program

#include <iostream>
using namespace std;

int main() {
    cout << "int :                " << sizeof(int) << endl;
    cout << "unsigned int :       " << sizeof(unsigned int) << endl;
    cout << "signed int :         " << sizeof(signed int) << endl;
    cout << "short int :          " << sizeof(short int) << endl;
    cout << "long int :           " << sizeof(long int) << endl;
    cout << "signed short int :   " << sizeof(signed short int) << endl;
    cout << "unsigned short int : " << sizeof(unsigned short int) << endl;
    cout << "signed long int :    " << sizeof(signed long int) << endl;
    cout << "unsigned long int :  " << sizeof(unsigned long int) << endl;
    cout << "long long :          " << sizeof(long long) << endl;
    cout << "unsigned long long : " << sizeof(unsigned long long) << endl << endl;

    cout << "char :          " << sizeof(char) << endl;
    cout << "unsigned char : " << sizeof(unsigned char) << endl;
    cout << "signed char :   " << sizeof(signed char) << endl << endl;

    cout << "bool : " << sizeof(bool) << endl << endl;

    cout << "float :   " << sizeof(float) << endl;
    cout << "double :  " << sizeof(double) << endl;
    cout << "wchar_t : " << sizeof(wchar_t) << endl;
}

Output

int :                4
unsigned int :       4
signed int :         4
short int :          2
long int :           8
signed short int :   2
unsigned short int : 2
signed long int :    8
unsigned long int :  8
long long :          8
unsigned long long : 8

char :          1
unsigned char : 1
signed char :   1

bool : 1

float :   4
double :  8
wchar_t : 4
Program ended with exit code: 0

Derived Datatypes

These are the datatypes that are derived from the primitive datatypes.

  • Function
  • Array
  • Pointer
  • Reference

User-defined Datatypes

These datatypes are defined by the user.

  • Class
  • Structure
  • Union
  • Enumeration
  • Typedef

We shall look into derived and user-defined datatypes in detail in the subsequent tutorials of this course.

Conclusion

In this C++ Tutorial, we learned what datatypes are in C++, different categories of datatypes, with examples.