C++ Data types3 min read

While writing a program, we store the information by creating a variable. These variables are nothing but the name given to some unspecified memory location. Now the information can be character type, integer type, double, float etc.

A data type specifies the type of data that a variable can store such as integer, floating, character, double, boolean etc. Each of these data types varies in storage size that will be shown below in the table.

A data type or simply type is an attribute of data that tells the compiler or interpreter how the programmer intends to use the data.

Types of data types in C++:

  • Primitive Data Types: integer, float, character, double, boolean, void and wchar_t.
  • Derived Data Types: Arrays, Pointers and Function.
  • Abstract or User-Defined Data Types: Structure, Union and Enum.

Primitive Data Types:

These are the predefined data types that are already defined in C++ such as integer, float, character, double, boolean, void and wchar_t.

Basic Built in Datatypes in C++:

The memory size of data types may change according to 32 or 64 bit Operating System (OS).

data typedesc and size
charfor character storage (1 byte)
intfor integral number (2 bytes)
floatsingle precision floating point (4 bytes)
doubledouble precision floating point numbers (8 bytes)
boolBoolean (True or False)
voidWithout any Value
wchar_tWide Character. (2 or 4 bytes)

**Wide Character(wchar_t): This type should be avoided because its size is implementation-defined and not reliable.

How above basic data type is written in code, example:

Modifiers in C++:

The modifiers for the built-in data is used specify them more precisely and even increase the range. Following modifiers are used with the above basic data types:

  1. signed: includes both positive and negative numbers.
  2. unsigned: refers to the numbers that are always without any sign, that are always positive.
  3. short: minimum values that a data type will hold.
  4. long: maximum values that a data type will hold.

Size hierarchy: short int < int < long int
Size hierarchy for floating point numbers is: float < double < long double

Data Types (s & u)Memory SizeRange
char1 byte-128 to 127
signed char1 byte-128 to 127
unsigned char1 byte0 to 127
short2 byte-32,768 to 32,767
signed short2 byte-32,768 to 32,767
unsigned short2 byte0 to 32,767
int2 byte-32,768 to 32,767
signed int2 byte-32,768 to 32,767
unsigned int2 byte0 to 32,767
short int2 byte-32,768 to 32,767
signed short int2 byte-32,768 to 32,767
unsigned short int2 byte0 to 32,767
long int4 byte
signed long int4 byte
unsigned long int4 byte
float4 byte
double8 byte
long double10 byte

Derived Data Types:

Also known as Non-Primitive data type. These are:

  • Arrays
  • Pointers
  • Function.

These topics are covered in detail in separate tutorials.


Abstract or User-Defined Data Types:

These are the type, that user creates as a class or a structure. There three types of user-defined data types in C++:

  • struct
  • union
  • enum

These topics are covered in detail in separate tutorials.


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More