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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …