Data Types In C++

Our today’s topic in learning C++ is data types. Data types are used in the C++ program to determine the type of data which needs to be stored in a variable. It also decides the size of the memory location is allocated for storing the data. Data types in C++ are classified into 3 different types:

  1. Built-in/primitive data type
    1. Integral type
      1. Integer(int)
      2. Character(char)
    2. Floating point 
      1. Floating point(float)
      2. Double floating point(double)
    3. Void
    4. Boolean
    5. Wide character
  2. User-defined data type
    1. Struct
    2. Union
    3. Class
    4. enumeration
  3. Derived data type
    1. Array 
    2. Reference
    3. Function
    4. Pointer

So, the pictorial representation of the classification would be like this:

data types in c 1
Data Types In C++

Starting with Primitive data type/ built-in data types, these are the data types that can be directly used by the user and these are not made by a combination of other data types. These are also called atomic data types as said above they are not composed of other data types.

Primitive data types are of 5 categories:

  1. Integral types: Integral data types consists of integers(used to store whole numbers) which are denoted by int and character type(used to store characters) which is denoted by char.
  2. Example:
int a= 15; char c='a';
  1. Floating types: These consist of floating-point data type(used to store decimal numbers and fractional numbers with single precision) denoted by the float and double floating-point data type(used to store float data with double precision) denoted by double.

Example:

float length= 12.54; double area= 32.34562;
  1. Void: It is used to specify the return type of function and it is also used indicate an empty argument list to a function.
  2. Boolean: These are used in conditional statements and loops as Booleans can only have one of the 2 values, it is either true or false.

Example:

bool value='true';
  1. Wide Character: These are used to store character type data which require more space than the usual character type as the wide character type is allocated with 2 bytes of memory instead of 1 byte. It is denoted by wchar_t.

Now, moving to derived data types, these are the data types that are derived from primitive data types. These are of 4 types:

  1. Array: An Array is a collection of homogeneous elements.

Example:

char arr[5]="shiva";
  1. Pointer: It points to a specific data location or address.

Example:

int *mp;
 mp=&y;
 *mp=3;
  1. Function: Functions are used to perform different tasks like to add values, to multiply, etc. The function starts with a function name followed by 2 brackets then the required statements are written and this function is called by an object.
  2. Reference: &

The basic primitive data types can be modified by using different Data type modifiers to be used for different purposes. There are 4 types of data type modifiers:

  1. Signed
  2. Unsigned 
  3. Long 
  4. Short

So, the following data shows different data types, storage space required for them and their range.

DATA TYPESPACE REQUIREDSPECIFIC RANGE
Int4 bytes-2147483648 to 2147483647
Signed int4 bytes-2147483648 to 2147483647
Unsigned int4 bytes0 to 4294967295
Short int2 bytes-32768 to 32767
Long int4 bytes-2147483648 to 2147483647
Unsigned short int2 bytes0 to 65535
Signed short int2 bytes-32768 to 32767
Unsigned long int4 bytes0 to 4294967295
Signed long int4 bytes-2147483648 to 2147483647
Char1 byte-128 to 127 or 0 to 255
Unsigned char1 byte0 to 255
Signed char1byte-128 to 127
Double8 bytes+/- 1.7e +/- 308 (~15 digits)
Long double8 bytes+/- 1.7e +/- 308 (~15 digits)
Float4 bytes+/- 3.4e +/- 38 (~7 digits)
wchar_t2 or 4 bytes1 wide character
Bool1 byteTrue or false
Different Data-Types

Example program to show the size of each data type:

#include <iostream>
using namespace std;
int main()
 {
 cout << "Size of int is " << sizeof(int) << endl;
 cout << "Size of short int is " << sizeof(short int) << endl;
 cout << "Size of long int is " << sizeof(long int) << endl;
 cout << "Size of char is " << sizeof(char) << endl;
 cout << "Size of double is " << sizeof(double) << endl;
 cout << "Size of float is " << sizeof(float) << endl;
 cout << "Size of wchar_t is " << sizeof(wchar_t) << endl;
 return 0;
}

Below screenshot shows the size of each data type used by the compiler:

6FsYESjwTmtzzTWxBGF4dNJPQfhFuX11eAT2MLFTBtJF 4AoBcK2aseOkkPkn8UEL43zvmDClGdkOGYoY1DwH52GvZqAmy CVqVK6vJeLvuGaiBMrnDlIQcdCXmAnrloRsM1eg
Screenshot Dedicated To Shiva Patra 

So, our last category of data types is user-defined data types, these are data types which are defined by the users for suitable purposes. These are of 4 types:

  1. Struct 
  2. Union 
  3. Enumeration
  4. Class

We didn’t discuss much the derived and user-defined data types to the root as they separate chapters in C++ regarding the same. To keep the article short and easy to grasp we have to end it here only. Thank You.

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.

Share Your Love
Default image
Shiva Patra
Hello, this is G. Shiva. Patra, your content writer for this website, is currently pursuing BCA for a graduation degree. I am here to write content based on C++ and similar kind of stuff. I like the tech world as it grows really fast and you have great things to learn every day which keep you away from boredom. So, I have some knowledge on very different topics related to tech like the dark web, deep web, onion rooting, IoT, Cybersecurity, and I always keep track of new devices and software used by them as every tech enthusiast does.
Articles: 9

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply