Home Tutorials C++ Tutorial Data Types
Data Types

Data Types


C++ Data Types

Definition: Data types define the type and size of data that a variable can store. When you declare a variable, the compiler reads its data type to determine exactly how many bytes of memory to reserve in RAM and how to interpret the binary bits stored inside that location.

Why: Data types form one of the absolute core pillars of any beginner C++ module. Because C++ is a strongly typed and performance-focused language, choosing the correct data type ensures that your program runs efficiently and manages system memory without wasting valuable hardware resources.


Example: Implementing Core Data Types

Below is a complete program demonstrating how to declare different data types and output their values onto the console screen:

#include <iostream>
#include <string> // Required to work with text strings
using namespace std;

int main() {
    int age = 20;                  // Integer type
    float marks = 88.5f;          // Floating-point type
    char grade = 'A';              // Character type (single quotes)
    bool pass = true;              // Boolean type (true or false)
    string name = "Ravi";          // String object type (double quotes)

    // Displaying values using standard output stream
    cout << age << endl;
    cout << marks << endl;
    cout << grade << endl;
    cout << pass << endl;
    cout << name << endl;

    return 0;
}

Detailed Breakdown of Primitive Data Types

Each type serves a unique mathematical or logical purpose. Choosing the right one makes your logic transparent and optimized:

Type Keyword Memory Footprint Syntax Rules & Use Case
int 4 bytes Stores basic counting whole numbers without decimals. Ideal for loop counters, age, or quantities.
float 4 bytes Stores fractional numbers up to 6–7 decimal digits. Always append an f at the end of the literal number (e.g., 88.5f) to avoid double conversion.
char 1 byte Stores a single character or literal symbol. Values must be encapsulated within single quotes (e.g., 'A'). Under the hood, it stores an integer matching the system's ASCII table code.
bool 1 byte Handles logical conditional flags. Accepts only true or false. When printed using cout, true prints as 1 and false prints as 0.
string Dynamic Part of the standard library namespace (std::string). Used to store compound alpha-numeric words or text phrases. Values must reside inside double quotes (e.g., "Ravi").

Key Notes

  • Data Type Modifiers: You can alter the memory range and behavior of primitive types using modifiers like short, long, signed, and unsigned (e.g., unsigned int cannot store negative numbers, which effectively doubles its positive capacity range).
  • Size Discovery: Memory sizes can vary slightly depending on your target system processor architecture. You can dynamically check how many bytes a type occupies on your system by using the built-in sizeof() operator, such as cout << sizeof(int);.
  • Type Safety: Assigning a floating-point value to an integer variable (e.g., int score = 95.8f;) forces the compiler to chop off the decimal remainder entirely. The variable will simply store 95, resulting in a loss of data precision.

🏋️ Test Yourself With Exercises

Take our quiz on Data Types to test your knowledge.

Browse Quizzes »