C++ Data Types – The Complete Guide
Did you know that C++ offers an extensive range of data types? This feature makes it a popular choice among programmers as a data type in any programming language is a prime necessity.
The data type is used by all the variables to limit the data that can be stored. When you define a variable in C++, the compiler allocates memory for it based on the data type with which it is declared. There is a broad range of C++ data types including floats, integers, strings, and boolean values. In this blog, we will learn about the different types of data types in C++ and understand it in-depth with elaborative examples.
Introduction to Data Types in C++
There are various types of data types that C++ offers to store data of different types. It has categories that are divided into subcategories in the explanation of data types in C++ with examples. The following are the categories in data types:
- Primitive Data Types
- Derived Data Types
- User Defined Data Types
In the upcoming sections, we will learn about the three categories of data types mentioned above in detail. You can also check this C++ couse to have a better understanding of the concepts.
Primitive Data Types
The primitive data types are used to store the basic values in C++. They are in-built and include the data types like character, integer, and boolean.
Following below is an explanation of the primitive data types:
Integer
This data type stores the integer values and can take up to 2 to 4 bytes of memory. The integer range is from -2147483648 to 2147483647. An example of an integer data type variable is as follows:
int data = 1467;
Character
This data type stores the character values and can take up to 1 byte of memory. The data here is a character in the data type variable. Following is an example of the character data type:
char ch = 'a';
Wide Character
The wide-character data type is the type that has character values and a memory size that is up to 2 to 4 bytes. It is mostly used to store the characters that take up more space and memory storage. The following is an example of the wide-character data type:
wchar_t w = S'K';
Boolean
The boolean data type stores the boolean values, which can be either true or false. It can take up to 1 byte of memory. The data here is in the boolean data type variable and the bool keyword is used in it. Following is an example of the boolean data type:
bool is_false = false;
Floating Point
This data type only stores the float values, which can either be decimals or exponential. It can take up to 4 bytes of memory space. The data available in the floating data type utilizes the float keyword. Following is an example of the floating data type:
float val = 32.24;
Double Floating Point
This data type stores the float values, which can either be decimals or exponential. There is only one difference between the floating and double-floating data types. The double floating data type has double precision and uses 8 bytes of memory. The following is an example of this data type:
double val = 2564.1436;
Void
This data type is used for the values that hold no significance and represent valueless data. The variables in this data type are not declared and do not return any data value.
Derived Data Types
The derived data types are composed of a combination of primitive data types. The following are four of its examples:
Function
The function is a part of code that is written in C++ programming language. They are mostly used when you want to write the same code repeatedly. It works by combining all the lines in the code and making sure that the function data type is returned after execution.
Syntax:
function_return_type function_name(parameters) {
}
Example:
int sum(int num1, int num2) {
return (num1 + num2);
}
Array
The array stores the data and values in sequential order in a single variable name and keeps the memory of elements in a continuous manner.
Syntax:
datatype array_name[size_of_array];
Example:
int arr[3]={0,1,2};
Pointer
The pointers in C++ store the value as addresses in the variables that are the same as the value of the pointer. It can store values with memory of up to 4 to 8 bytes. It completely changes the structure of the program and makes it look more presentable.
Syntax:
data_type* variable_name;
Example:
int* point_int;
Reference
The reference becomes an alternate name for variables when you declare it. You can add a reference after adding ‘&’ to the variable and declaring it. An example of the same is as follows:
int val = 1345;
int &ref = val;
The reference ‘ref’ has replaced the integer ‘val’ and it will represent the changes automatically to all the other values.
User-Defined Data Types
This category of C++ data types is defined by the users, hence they are called user-defined data types. The following are a few examples:
Structure
The structure is a user-defined data type that is used to store or bind two or more data types together. It will allow you to store data in an efficient and organized way.
Syntax:
struct structure_name
{
Int a;
Char b;
}
Example:
struct student {
int[a];
char[b];
int marks;
};
Class
The class is a user-defined data type and is used to construct pieces of code that lead to object-oriented programming. The following is an example of the same:
#include <bits/stdc++.h>
using namespace std;
class scaler {
public:
string student_name;
void print_name() {
cout << "Student name is: " << student_name << endl;
}
};
int main() {
scaler student1, student2;
student1.student_name = "A";
student1.print_name();
student2.student_name = "B";
student2.print_name();
return 0;
}
The above-mentioned example will provide an output that gives,
Student name is: A
Student name is: B
Union
The Union is very similar to the Structure as it is used to combine the different data types into one single data type. The data types have the same memory to access. The following is an example of the same:
union test {
int num;
char var;
};
Conclusion
We have discussed everything about C++ data types with the categories and different examples. The variety of data types that C++ offers makes it the most extensively used programming language among users. You can understand these data types in order to work with efficiency in C++.