Data types specify the type of data that a variable can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared as every data type requires a different amount of memory.
C++ supports a wide variety of data types, and the programmer can select the data type appropriate to the needs of the application.
Example:
C++
#include <iostream>
using namespace std;
int main() {
// Creating a variable to store integer
int var = 10;
cout << var;
return 0;
}
Explanation: In the above code, we needed to store the value 10 in our program, so we created a variable var. But before var, we have used the keyword 'int'. This keyword is used to define that the variable var will store data of type integer.
Classification of Datatypes
In C++, different data types are classified into the following categories:
S. No. | Type | Description | Data Types |
---|
1 | Basic Data Types | Built-in or primitive data types that are used to store simple values. | int, float, double, char, bool, void |
---|
2 | Derived Data Types | Data types derived from basic types. | array, pointer, reference, function |
---|
3 | User Defined Data Types | Custom data types created by the programmer according to their need. | class, struct, union, typedef, using |
---|
Let's see how to use some primitive data types in C++ program.
1. Character Data Type (char)
The character data type is used to store a single character. The keyword used to define a character is char. Its size is 1 byte, and it stores characters enclosed in single quotes (' '). It can generally store upto 256 characters according to their ASCII codes.
Example:
C++
#include <iostream>
using namespace std;
int main() {
// Character variable
char c = 'A';
cout << c;
return 0;
}
2. Integer Data Type (int)
Integer data type denotes that the given variable can store the integer numbers. The keyword used to define integers is int. Its size is 4-bytes (for 64-bit) systems and can store numbers for binary, octal, decimal and hexadecimal base systems in the range from -2,147,483,648 to 2,147,483,647.
Example:
C++
#include <iostream>
using namespace std;
int main() {
// Creating an integer variable
int x = 25;
cout << x << endl;
// Using hexadecimal base value
x = 0x15;
cout << x;
return 0;
}
To know more about different base values in C++, refer to the article - Literals in C++
3. Boolean Data Type (bool)
The boolean data type is used to store logical values: true(1) or false(0). The keyword used to define a boolean variable is bool. Its size is 1 byte.
Example:
C++
#include <iostream>
using namespace std;
int main() {
// Creating a boolean variable
bool isTrue = true;
cout << isTrue;
return 0;
}
4. Floating Point Data Type (float)
Floating-point data type is used to store numbers with decimal points. The keyword used to define floating-point numbers is float. Its size is 4 bytes (on 64-bit systems) and can store values in the range from 1.2e-38 to 3.4e+38.
Example:
C++
#include <iostream>
using namespace std;
int main() {
// Floating point variable with a decimal value
float f = 36.5;
cout << f;
return 0;
}
5. Double Data Type (double)
The double data type is used to store decimal numbers with higher precision. The keyword used to define double-precision floating-point numbers is double. Its size is 8 bytes (on 64-bit systems) and can store the values in the range from 1.7e-308 to 1.7e+308
Example:
C++
#include <iostream>
using namespace std;
int main() {
// double precision floating point variable
double pi = 3.1415926535;
cout << pi;
return 0;
}
6. Void Data Type (void)
The void data type represents the absence of value. We cannot create a variable of void type. It is used for pointer and functions that do not return any value using the keyword void.
Type Safety in C++
C++ is a strongly typed language. It means that all variables' data type should be specified at the declaration, and it does not change throughout the program. Moreover, we can only assign the values that are of the same type as that of the variable.
Example: If we try to assign floating point value to a boolean variable, it may result in data corruption, runtime errors, or undefined behaviour.
C++
#include <iostream>
using namespace std;
int main() {
// Assigning float value to isTrue
bool a = 10.248f;
cout << a;
return 0;
}
As we see, the floating-point value is not stored in the bool variable a. It just stores 1. This type checking is not only done for fundamental types, but for all data types to ensure valid operations and no data corruptions.
Data Type Conversion
Type conversion refers to the process of changing one data type into another compatible one without losing its original meaning. It's an important concept for handling different data types in C++.
Example:
C++
#include <iostream>
using namespace std;
int main() {
int n = 3;
char c = 'C';
// Convert char data type into integer
cout << (int)c << endl;
int sum = n + c;
cout << sum;
return 0;
}
Size of Data Types in C++
Earlier, we mentioned that the size of the data types is according to the 64-bit systems. Does it mean that the size of C++ data types is different for different computers?
Actually, it is partially true. The size of C++ data types can vary across different systems, depending on the architecture of the computer (e.g., 32-bit vs. 64-bit systems) and the compiler being used. But if the architecture of the computer is same, then the size across different computers remains same.
We can find the size of the data type using sizeof operator. According to this type, the range of values that a variable of given data types can store are decided.
Example:
C++
#include <iostream>
using namespace std;
int main() {
// Printing the size of each data type
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes";
return 0;
}
OutputSize of int: 4 bytes
Size of char: 1 byte
Size of float: 4 bytes
Size of double: 8 bytes
Data Type Modifiers
Data type modifiers are the keywords used to change or give extra meaning to already existing data types. It is added to primitive data types as a prefix to modify their size or range of data they can store. There are 4 type modifiers in C++: short, long, signed and unsigned.
For Example, defining an int with long type modifier will change its size to 8 bytes:
C++
int var1; // 4 bytes
long int var2; // 8 bytes
Similarly, other type modifiers also affect the size or range of the data type.
long double, long long int, unsigned int, etc.
Similar Reads
C++ Tutorial | Learn C++ Programming C++ is a popular programming language that was developed as an extension of the C programming language to include OOPs programming paradigm. Since then, it has become foundation of many modern technologies like game engines, web browsers, operating systems, financial systems, etc.Features of C++Why
5 min read
Introduction to c++
Difference between C and C++C++ is often viewed as a superset of C. C++ is also known as a "C with class" This was very nearly true when C++ was originally created, but the two languages have evolved over time with C picking up a number of features that either weren't found in the contemporary version of C++ or still haven't m
3 min read
Setting up C++ Development EnvironmentC++ is a general-purpose programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented, and generic programming features. C++ runs on lots of platforms like Windows, Linux, Unix, Mac, etc. Before we start programming with C++. We will need an enviro
8 min read
Header Files in C++C++ offers its users a variety of functions, one of which is included in header files. In C++, all the header files may or may not end with the ".h" extension unlike in C, Where all the header files must necessarily end with the ".h" extension. Header files in C++ are basically used to declare an in
6 min read
Namespace in C++Name conflicts in C++ happen when different parts of a program use the same name for variables, functions, or classes, causing confusion for the compiler. To avoid this, C++ introduce namespace.Namespace is a feature that provides a way to group related identifiers such as variables, functions, and
6 min read
Writing First C++ Program - Hello World ExampleThe "Hello World" program is the first step towards learning any programming language and is also one of the most straightforward programs you will learn. It is the basic program that demonstrates the working of the coding process. All you have to do is display the message "Hello World" on the outpu
4 min read
Basics
C++ Data TypesData types specify the type of data that a variable can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared as every data type requires a different amount of memory.C++ supports a wide variety of data typ
7 min read
C++ VariablesIn C++, variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be accessed or changed during program execution.Creating a VariableCreating a variable and giving it a name is called variable definition (sometimes called variable
4 min read
Operators in C++C++ operators are the symbols that operate on values to perform specific mathematical or logical computations on given values. They are the foundation of any programming language.Example:C++#include <iostream> using namespace std; int main() { int a = 10 + 20; cout << a; return 0; }Outpu
9 min read
Basic Input / Output in C++In C++, input and output are performed in the form of a sequence of bytes or more commonly known as streams.Input Stream: If the direction of flow of bytes is from the device (for example, Keyboard) to the main memory then this process is called input.Output Stream: If the direction of flow of bytes
5 min read
Control flow statements in ProgrammingControl flow refers to the order in which statements within a program execute. While programs typically follow a sequential flow from top to bottom, there are scenarios where we need more flexibility. This article provides a clear understanding about everything you need to know about Control Flow St
15+ min read
C++ LoopsIn C++ programming, sometimes there is a need to perform some operation more than once or (say) n number of times. For example, suppose we want to print "Hello World" 5 times. Manually, we have to write cout for the C++ statement 5 times as shown.C++#include <iostream> using namespace std; int
7 min read
Functions in C++A function is a building block of C++ programs that contains a set of statements which are executed when the functions is called. It can take some input data, performs the given task, and return some result. A function can be called from anywhere in the program and any number of times increasing the
9 min read
C++ ArraysIn C++, an array is a derived data type that is used to store multiple values of similar data types in a contiguous memory location.Arrays in C++Create an ArrayIn C++, we can create/declare an array by simply specifying the data type first and then the name of the array with its size inside [] squar
10 min read
Strings in C++In C++, strings are sequences of characters that are used to store words and text. They are also used to store data, such as numbers and other types of information in the form of text. Strings are provided by <string> header file in the form of std::string class.Creating a StringBefore using s
5 min read
Core Concepts
Pointers and References in C++In C++ pointers and references both are mechanisms used to deal with memory, memory address, and data in a program. Pointers are used to store the memory address of another variable whereas references are used to create an alias for an already existing variable. Pointers in C++ Pointers in C++ are a
5 min read
new and delete Operators in C++ For Dynamic MemoryIn C++, when a variable is declared, the compiler automatically reserves memory for it based on its data type. This memory is allocated in the program's stack memory at compilation of the program. Once allocated, it cannot be deleted or changed in size. However, C++ offers manual low-level memory ma
6 min read
Templates in C++C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so
9 min read
Structures, Unions and Enumerations in C++Structures, unions and enumerations (enums) are 3 user defined data types in C++. User defined data types allow us to create a data type specifically tailored for a particular purpose. It is generally created from the built-in or derived data types. Let's take a look at each of them one by one.Struc
3 min read
Exception Handling in C++In C++, exceptions are unexpected problems or errors that occur while a program is running. For example, in a program that divides two numbers, dividing a number by 0 is an exception as it may lead to undefined errors.The process of dealing with exceptions is known as exception handling. It allows p
11 min read
File Handling through C++ ClassesIn C++, programs run in the computerâs RAM (Random Access Memory), in which the data used by a program only exists while the program is running. Once the program terminates, all the data is automatically deleted. File handling allows us to manipulate files in the secondary memory of the computer (li
8 min read
Multithreading in C++Multithreading is a technique where a program is divided into smaller units of execution called threads. Each thread runs independently but shares resources like memory, allowing tasks to be performed simultaneously. This helps improve performance by utilizing multiple CPU cores efficiently. Multith
5 min read
C++ OOPS
Standard Template Library (STL)
Practice Problem