C++ Data Types

Every variable in the C++ programming language must have a specified data type because it is a strongly typed language. Many built-in data types are available in C++ that can be used to store various kinds of values. Some of the most popular data types in C++ are listed below:

Data Type
Format Specifier
Size (in bytes)
char
%c
1
short
%hd
2
int
%d
4
long
%ld
4 or 8
float
%f
4
double
%lf
8
long double
%LF
12 or 16
bool
%d or %s
1
long long
%lld
8

Integer Data Type:

Whole numbers are stored using integers. The integer data types in C++ are int, short, long, and long long. The system architecture and the compiler determine the size and scope of these data types.

Example:

float a = 3.14f;
double b = 3.14159265358979323846;

Note that the f after 3.14 is required to tell the compiler that this is a float value. Otherwise, the compiler will treat it as a double.

Character Data Type:

A single character is stored using the char data type. It could be a special character, a numeral, a letter, or a punctuation mark. Characters are encapsulated in single quotes (‘) in C++.

char c = 'A';

Boolean Data Type:

Boolean values, which can only be true or false, are stored using the bool data type.

bool isTrue = true;
bool isFalse = false;

These are only a few of the C++ data types that are most frequently used. You can also utilise other data types, such as enum, struct, union, and others. To guarantee that your variables can store the values you require and to prevent memory waste, it’s critical to select the appropriate data type for them.