C++ Data Types

Understanding different types of data in C++

🗂️ What are C++ Data Types?

Data types specify what kind of data a variable can store. C++ has built-in types like integers for whole numbers, doubles for decimals, strings for text, and booleans for true/false values.


#include <iostream>
using namespace std;

int main() {
    int age = 25;              // Integer
    double price = 19.99;      // Decimal number
    char grade = 'A';          // Single character
    bool isStudent = true;     // True or false
    string name = "Alice";     // Text
    
    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
    cout << "Price: $" << price << endl;
    cout << "Grade: " << grade << endl;
    cout << "Student: " << isStudent << endl;
    
    return 0;
}
                                    

Output:

Name: Alice
Age: 25
Price: $19.99
Grade: A
Student: 1

Main Data Types

🔢

Integer Types

Whole numbers without decimals

int age = 25;
short year = 2024;
long population = 8000000;
🔸

Floating Point

Numbers with decimal points

float temperature = 98.6f;
double price = 19.99;
long double pi = 3.14159265359;
🔤

Character Types

Single characters and text

char letter = 'A';
string message = "Hello World";
wchar_t unicode = L'€';

Boolean Type

True or false values

bool isActive = true;
bool isComplete = false;
bool result = (5 > 3);

🔹 Integer Data Types

C++ offers several integer types—short, int, long, and unsigned—each with distinct ranges and memory usage. Choose short (2 bytes) for small numbers up to 32,000, int (4 bytes) for values like 2,000,000, and long (8 bytes) for large figures such as 9,000,000,000. Unsigned types (e.g., 4,000,000,000) store only non-negative values, doubling the positive range. Selecting the appropriate type optimizes memory and performance, preventing overflow errors and ensuring efficient data handling in applications from embedded systems to large-scale computations.

#include <iostream>
using namespace std;

int main() {
    // Different integer types
    short smallNumber = 32000;        // -32,768 to 32,767
    int regularNumber = 2000000;      // -2 billion to 2 billion (approx)
    long bigNumber = 9000000000L;     // Even larger range
    
    // Unsigned integers (only positive)
    unsigned int positiveOnly = 4000000000U;
    
    cout << "Short: " << smallNumber << endl;
    cout << "Int: " << regularNumber << endl;
    cout << "Long: " << bigNumber << endl;
    cout << "Unsigned: " << positiveOnly << endl;
    
    // Size of data types
    cout << "\nSizes in bytes:" << endl;
    cout << "short: " << sizeof(short) << endl;
    cout << "int: " << sizeof(int) << endl;
    cout << "long: " << sizeof(long) << endl;
    
    return 0;
}

Output:

Short: 32000
Int: 2000000
Long: 9000000000
Unsigned: 4000000000

Sizes in bytes:
short: 2
int: 4
long: 8

🔹 Floating Point Data Types

Floating-point types in C++ (float, double) handle decimal numbers with precision for scientific, financial, or graphical applications. They store values like temperatures (98.60°F), prices ($19.99), mathematical constants (π ≈ 3.1415926536), and large numbers (1,230,000.0000000000). double provides greater precision than float but uses more memory. Proper use avoids rounding errors in calculations, ensuring accuracy in simulations, engineering models, and real-time data processing. Understanding floating-point representation is key to effective numerical programming in C++.

#include <iostream>
#include <iomanip>  // For precision control
using namespace std;

int main() {
    // Different floating point types
    float temperature = 98.6f;           // 7 digits precision
    double price = 19.99;                // 15 digits precision  
    long double pi = 3.14159265358979L;  // 19 digits precision
    
    // Set precision for output
    cout << fixed << setprecision(2);
    
    cout << "Temperature: " << temperature << "°F" << endl;
    cout << "Price: $" << price << endl;
    
    cout << setprecision(10);
    cout << "Pi: " << pi << endl;
    
    // Scientific notation
    double largeNumber = 1.23e6;  // 1.23 * 10^6 = 1,230,000
    cout << "Large number: " << largeNumber << endl;
    
    return 0;
}

Output:

Temperature: 98.60°F
Price: $19.99
Pi: 3.1415926536
Large number: 1230000.0000000000

🔹 Character and String Types

C++ uses char for single characters and string (from the STL) for text sequences, enabling versatile text manipulation. A char stores symbols like 'A', '5', or '@', while string handles phrases such as "John Smith" or "Learning C++ is fun!" Strings include useful methods for finding length (e.g., 10 characters), concatenation, and comparison. These types are fundamental for input/output operations, user interfaces, file processing, and data parsing. Mastery of characters and strings is essential for developing applications ranging from simple consoles to complex text processors.

#include <iostream>
using namespace std;

int main() {
    // Single characters
    char letter = 'A';
    char digit = '5';
    char symbol = '@';
    
    // Strings (multiple characters)
    string firstName = "John";
    string lastName = "Smith";
    string fullName = firstName + " " + lastName;  // String concatenation
    
    // String with spaces
    string sentence = "Learning C++ is fun!";
    
    cout << "Letter: " << letter << endl;
    cout << "Digit: " << digit << endl;
    cout << "Symbol: " << symbol << endl;
    cout << "Full name: " << fullName << endl;
    cout << "Sentence: " << sentence << endl;
    
    // String length
    cout << "Name length: " << fullName.length() << " characters" << endl;
    
    return 0;
}

Output:

Letter: A
Digit: 5
Symbol: @
Full name: John Smith
Sentence: Learning C++ is fun!
Name length: 10 characters

🔹 Boolean Data Type

The bool type in C++ represents true (1) or false (0) values, forming the basis of logical operations and control flow. It’s used for flags like online status (true), task completion (false), age checks (is_adult: true), or condition evaluations. Booleans streamline decision-making in code, such as enabling features only when conditions are met. They improve readability when paired with descriptive variable names (e.g., isValid) and are crucial in algorithms, state machines, and user input validation, making programs more intuitive and efficient.

#include <iostream>
using namespace std;

int main() {
    // Boolean variables
    bool isOnline = true;
    bool isComplete = false;
    
    // Boolean from comparisons
    int age = 18;
    bool isAdult = (age >= 18);
    bool isChild = (age < 13);
    
    cout << "Online status: " << isOnline << endl;
    cout << "Task complete: " << isComplete << endl;
    cout << "Is adult: " << isAdult << endl;
    cout << "Is child: " << isChild << endl;
    
    // Using boolalpha to show true/false instead of 1/0
    cout << boolalpha;
    cout << "Online (text): " << isOnline << endl;
    cout << "Complete (text): " << isComplete << endl;
    
    return 0;
}

Output:

Online status: 1
Task complete: 0
Is adult: 1
Is child: 0
Online (text): true
Complete (text): false

🧠 Test Your Knowledge

Which data type is best for storing a person's age?