C++ Data Types

In C++, a data type specifies the type of data that a variable can store. Different data types require different amounts of memory and represent different kinds of values.

Basic Data Types

The following table shows some of the most commonly used data types in C++.

Data TypeDescriptionExample
intUsed to store integer numbersint age = 25;
floatUsed to store decimal numbersfloat price = 10.5;
doubleUsed to store large decimal numbers with more precisiondouble pi = 3.14159;
charUsed to store a single characterchar grade = ‘A’;
boolUsed to store true or false valuesbool isActive = true;
stringUsed to store textstring name = “John”;

Example Program

#include <iostream>
#include <string>
using namespace std;

int main() {

    int age = 25;
    float price = 19.99;
    char grade = 'A';
    bool isStudent = true;
    string name = "Alice";

    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

Important Notes

  • Each variable must have a data type.
  • Data types define the size and type of values that can be stored.
  • Choosing the correct data type helps improve program efficiency.

Next Topic

Next, learn about C++ Constants.