C++ Datatypes

What is a Datatype ?

Datatypes are used to define or declare the type of data the variable is holding. This is used to determine the size of data and type the variable is associated.

Datatypes in C++ can be categorized in three types :

  • Fundamental Datatype
  • Derived Datatype
  • User - defined Datatype

1. Fundamental Datatypes :

    The fundamental datatypes are the datatypes that are predefined and can be used directly by the user in order to declare the variables in the program.

Fundamental datatypes


Types of fundamental datatypes :

  • Integer : The integer datatype is declared using the keyword int. The size of the int is usually 4 bytes. It can store values from -2147483648 to 2147483647.

  • Character : The character datatype is declared using the keyword char. The size of the char is usually 1 byte. The characters used in C++ should be enclosed under single quotes ( ' ' ).

  • Floating point : The floating point datatype is declared using the keyword float. The size of the float is usually 4 bytes. This datatype is used to store the single - precision float values or the decimal values.

  • Double floating point : The double floating point datatype is declared using the keyword double. The size of double is 8 bytes. Hence, double has two times the precision of float. This datatype is used for storing double - precision float values or the decimal values.

  • Boolean : The boolean datatype is declared using the keyword bool. The bool datatype stores one of the two possible values i.e either true or false. The booleans are used in the loops and conditional statements.

  • void : The void datatype is declared using the keyword void. The void datatype is used to declare or represent the valueless entity. void means without any value.

2. Derieved Datatypes :

    The datatypes that are reffered from the built - in or primitive datatypes can be known as the derieved datatypes.


Types of derieved datatypes:

  • Array : An array can be known as the collection of similar type of elements or data. The size of the array is fixed we can neither increase or decrease the size of array. The indexing of the array always begin from 0 and ends with 1 less than the size of array.

  • Functions : A function is known as the block of code or program - segment that is to perform a specific task. It helps the user to prevent from writing a same code again and again. All the lines of code are written inside a function and then can be called from anywhere.

  • Pointers : A pointer is a variable that stores the address of the another variable. It can be known as the symbolic representation of a address.

  • Reference : A variable can be declared as reference by using '&' in the declaration of the variable. By declaring a variable as reference, it becomes as an alternative name for the existing variable.

3. User - defined Datatypes :

    The user - defined datatypes can be known as the datatypes that are defined by the user itself.


Types of User - defined datatypes :

  • Class : The Class is used to hold its own data members and member functions, which can be used or accessed by creating the instance of that class. Class is known as the building block of C++ that leads to object - oriented programming.

  • Structure : The structure is used to store different datatypes into a single place. The keyword used to denote the structure is struct. Each variable of the structure is known as the member of the structure. 

  • Union : In union all the members share the same memory location. It is same like structure, can hold different datatypes. Size of the union is decided by the member having  the largest size.

  • Enum : The enum or enumeration is used to give or assign the names to the integral constants like elements, members, etc. It makes the program easy to read and maintain it.