Introduction of C++ Data Types

In this blog post, we will learn about C++ data types with the help of examples. But before the classification first let’s understand what is data type.

A data type is a classification of data that tells the compiler or interpreter how the programmer intends to use the data. At a high level, we can divide data types into three categories, Built-in types, Derived types, and User-defined types.

In C++, data types can be classified as follows:

  • Primary or Built-in or Fundamental data type.
  • Derived data types.
  • User-defined data types

C++ Data types

 

Built-in types (C++):

Built-in types define by the C++ language standard and these are built into the compiler. Built-in types are divided into three main categories: integral, floating-point, and void. Let’s see some built-in data types which are frequently used by programmers,

  • Integer
  • Character
  • Boolean
  • Floating Point
  • Double Floating Point
  • void
  • Wide Character

 

Derived types in C++:

Derived data types are derived from Built-in data types or pre-defined data types. Let’s see some derived data types,

  • Arrays types.
  • Pointers types.
  • Reference types.
  • function types.

 

User-defined data types in C++:

C++ language also provides flexibility to the programmer to create their own data types. Let’s see some user-defined types,

  • Class.
  • Structure.
  • Union.
  • Enumeration.
  • Typedef defined DataType.

 

Datatype Modifiers in C++:

The modifier modifies the basic integer type. Also, it can be mixed in any order and only one of each group can be present in the type name. They are as follows,

Note: as with all type specifiers, any order is permitted; so,  unsigned short int and short unsigned int name the same type similarly also unsigned long long int and long int unsigned long name the same type.

Signedness:

It can further be divided into two categories signed and unsigned.

signed:

The target type will have a signed representation. There are five standard signed integer types: “signed char”, “short int”, “int”, “long int”, and “long long int”.

Each type in this list provides at least as much storage as the types that came before it. For example, “int” is capable of storing the value of “short int” but vice versa is not guaranteed true.

There could also be extended signed integer types that are implementation-defined. Extended and standard signed integer types are collectively called signed integer types.

unsigned:

The target type will have an unsigned representation. For each of the standards signed integer types, there exists a corresponding (but different) standard unsigned integer type.

They are the following, “unsigned char”, “unsigned short int”, “unsigned int”, “unsigned long int”, and “unsigned long long int”.

Likewise, for each of the extended signed integer types, there exists a corresponding extended unsigned integer. Also the extended and standard unsigned integer types are collectively called unsigned integer types.

One point that you should remember is that the width of an unsigned integer type has the same width N as the corresponding signed integer type. That means the size of “signed int” and “unsigned int” would be the same.

See the below example code,

#include <stdio.h>

int main()
{
    signed char ich;
    unsigned char uch;
    signed int iint;
    unsigned int uint;
    long int lint;
    unsigned long int ulint;

    printf("Size of signed char %u\n", sizeof(ich));
    printf("Size of usigned char %u\n", sizeof(uch));

    printf("Size of signed int %u\n", sizeof(iint));
    printf("Size of usigned int %u\n", sizeof(uint));

    printf("Size of signed long int %u\n", sizeof(lint));
    printf("Size of unsigned long int %u\n", sizeof(uint));

    return 0;
}

Output:

Size of signed char 1
Size of usigned char 1
Size of signed int 4
Size of usigned int 4
Size of signed long int 4
Size of unsigned long int 4

 

Note: Overflow for signed arithmetic yields undefined behavior but unsigned arithmetic does not overflow. For more detail, you can check my other post, “signed vs unsigned“.

 

Size:

Following are the size modifiers.

short:

The target type will be optimized for space and will have a width of at least 16 bits.

long:

The target type will have a width of at least 32 bits.

long long:

The target type will have a width of at least 64 bits. (since C++11)

sizeof signed int cpp

Now I believe you would be able to understand the size modifier but you should remember that the width of each signed integer type shall not be less than the values specified in the below table.

Signed int width

A signed or unsigned integer type’s value representation consists of N bits, where N is the width.

 

The following table illustrates all available integer types and their properties in various common data models:

Type specifier Equivalent type Width in bits by data model
C++ standard LP32 ILP32 LLP64 LP64
signed char
signed char at least
8
8 8 8 8
unsigned char
unsigned char
short
short int at least
16
16 16 16 16
short int
signed short
signed short int
unsigned short
unsigned short int
unsigned short int
int
int at least
16
16 32 32 32
signed
signed int
unsigned
unsigned int
unsigned int
long
long int at least
32
32 32 32 64
long int
signed long
signed long int
unsigned long
unsigned long int
unsigned long int
long long
long long int
(C++11)
at least
64
64 64 64 64
long long int
signed long long
signed long long int
unsigned long long
unsigned long long int
(C++11)
unsigned long long int

Recommended Articles for you: