Data Types in C++

In this tutorial, we will learn about the data types in C++. Data types are often used during the variable declaration. They tell us about the type of data a variable can store. Let’s understand these data types in detail.

Data Types in C++

Data types are used to define the type of data for a variable. There are three types of data types in C++.

  • Primitive data type
  • Derived data type
  • User-defined data type

Let’s discuss them separately.

Primitive Data Types

These are the in-built data types and can be used directly by the user. There are following types of primitive data types in C++.

1. Integer: We use ‘int’ keyword to declare a variable as an integer. It can store all types of integers. Data Type Modifiers in C++, can be used for different ranges of integer values and size.

2. Character: We can use the ‘char’ keyword to declare a variable as a character. A string can be declared as an array of characters. Its size is 1 byte.

3. Float: ‘float’ keyword is used to store single-precision floating-point value in a variable. its size is 4 bytes.

4. Double: We use the ‘double’ keyword to store double-precision floating-point values in a variable. Its size is typically 8 bytes which is more than the size of the float.

5. Boolean: Keyword ‘bool’ is used to assign a boolean value to a variable. A boolean value can either be true or false.

6. Void: This keyword is used to tell the compiler that nothing is to be stored in the variable. A void value represents absence of any value. It has no size.

7. Wide character: A wide character is much like a character data type. The size of a wide-character is 2 or 4 bytes which is greater than a normal character variable. The keyword for this is ‘wchar_t’.

The use of these primitive data types has been illustrated in the given example program.

Example program:

#include <iostream>

int main()
{
  std::cout<<"size of int is: "<<sizeof(int)<<"\n";
  std::cout<<"size of char is: "<<sizeof(char)<<"\n";
  std::cout<<"size of float is: "<<sizeof(float)<<"\n";
  std::cout<<"size of double is: "<<sizeof(double)<<"\n";
  std::cout<<"size of bool is: "<<sizeof(bool)<<"\n";
  std::cout<<"size of wchar_t is: "<<sizeof(wchar_t)<<"\n";
  
  return 0;
}

Output:

size of int is: 4
size of char is: 1
size of float is: 4
size of double is: 8
size of bool is: 1
size of wchar_t is: 2

Derived data types

Derived data types are derived from the primitive data types which are discussed above. There are these following types of derived data types.

1. Array: An array is a collection of elements of similar types stored at contiguous memory locations.

2. Function: A function is a block of code written to perform some specific task.

3. Pointer and reference: Pointers are used to point to addresses of different variables. Referencing is used to get the address of a variable. Read more about it here: Pointers and references in C++.

User-defined data Types

C++ also provides ways for the user to define their own data types. This can be done using the following methods.

1. Class: A class in C++, is defined using class keyword. Classes are the entry point to object-oriented programming in C++. It has its own methods and constructors.

2. Structure: Structures are a group of items that can or cannot be of different types. We use ‘struct’ keyword to define such a group of items. For more, see this: Structures in C++

3. Union: Like a structure union is also a user-defined structure. The same memory location is shared by all the members here. Hence change in the value of one variable changes the value of others too. The keyword used for union is ‘union’.

4. Enumeration: Enumeration is also a user-defined data type in C++. The main purpose of using enumeration is to assign names to integral constants as names are easy to deal with. We use ‘enum’ keyword’ for this user-defined data type.

5. Typedef: We can use ‘typedef’ keyword to define new names for data types. We can use these new names anywhere in our program.

Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *