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.
- int: Used to store integer values.
- short: A smaller integer, typically 2 bytes.
- long: A larger integer, typically 4 or 8 bytes depending on the platform.
- long long: An even larger integer, typically 8 bytes.
- unsigned int: Used to store non-negative integers (positive integers and zero).
2. Floating-Point Types
Floating-point types are used to represent numbers with a fractional part (decimals).
- float: Stores 4-byte floating-point numbers.
- double: Stores 8-byte floating-point numbers (more precision than
float
). - long double: Stores extended precision floating-point numbers, usually 8 or 12 bytes depending on the platform.
3. Character Type
The char data type is used to store individual characters, typically 1 byte in size.
- char: Stores a single character (e.g., 'A', 'B', '3').
- unsigned char: Stores a single character, but can represent a larger range of values (0-255).
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:
- Arrays: A collection of elements of the same type.
- Pointers: Variables that store memory addresses.
- References: An alias for another variable.
- Structures: A user-defined data type that groups different data types together.
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
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).