Introduction to C++ | Data Types

C++ Data Types

In C++, data types define the type and size of data that can be stored in variables. Understanding C++ data types is essential for working with variables, performing calculations, and managing memory efficiently.

Primitive Data Types

C++ has several built-in primitive data types. These types are the basic building blocks of C++ programs and are divided into the following categories:

1. Integer Types

Integer types store whole numbers (positive or negative). The size of these types depends on the system, but they are typically 4 bytes.

2. Floating-Point Types

Floating-point types are used to represent numbers with a fractional part (decimals).

3. Character Type

The char data type is used to store individual characters, typically 1 byte in size.

4. Boolean Type

The bool data type is used to represent truth values. It can store either true or false.

5. Void Type

The void data type represents the absence of data. It is often used for functions that do not return a value.

Derived Data Types

In addition to the primitive data types, C++ also allows the creation of derived data types such as:

Code Example: Using Data Types in C++

Here’s an example demonstrating the use of various data types in a C++ program.

Code Example


#include <iostream>
using namespace std;

int main() {
int a = 10;
float b = 20.5;
double c = 30.123456;
char d = 'C';
bool isCPlusPlus = true;
long long e = 1234567890;

cout << "Integer a: " << a << endl;
cout << "Float b: " << b << endl;
cout << "Double c: " << c << endl;
cout << "Character d: " << d << endl;
cout << "Boolean isCPlusPlus: " << isCPlusPlus << endl;
cout << "Long long e: " << e << endl;

return 0;
}
        

Output

Integer a: 10
Float b: 20.5
Double c: 30.123456
Character d: C
Boolean isCPlusPlus: 1
Long long e: 1234567890

Pro Tip:

💡 Pro Tip

When choosing a data type in C++, consider the range of values and the required precision for your variables. For example, use int for whole numbers, float or double for decimal numbers, and char for single characters. The bool type is ideal for flagging conditions (true/false).