The Basic Data Types

C++ needs to know what kind of value each variable holds. Here are the types you will use most:

TypeWhat it holdsExample
intWhole numbersint year = 2025;
doubleNumbers with a decimal pointdouble price = 9.99;
charOne single character, in single quoteschar grade = 'A';
booltrue or falsebool isOpen = true;
stringText, in double quotesstring name = "Asha";
#include <iostream>
#include <string>
using namespace std;

int main() {
    int year = 2025;
    double price = 9.99;
    char grade = 'A';
    bool isOpen = true;
    string name = "Asha";

    cout << "Year: " << year << "\n";
    cout << "Price: " << price << "\n";
    cout << "Grade: " << grade << "\n";
    cout << "Open? " << isOpen << "\n";
    cout << "Name: " << name << "\n";
    return 0;
}

Output

Year: 2025
Price: 9.99
Grade: A
Open? 1
Name: Asha

Notice two things:

  • To use string, add #include <string> at the top.
  • bool prints as 1 for true and 0 for false.

Tip: Want true/false printed as words? Send boolalpha first: cout << boolalpha << isOpen; prints true.

Single quotes vs double quotes

This trips up a lot of beginners:

  • 'A' – single quotes – is a char: exactly one character.
  • "A" – double quotes – is a string: text (which can be one character, many, or none).

Whole numbers vs decimals

An int cannot hold a decimal part. If you put one in, the part after the point is simply cut off:

#include <iostream>
using namespace std;

int main() {
    int whole = 7.9;       // the .9 is thrown away
    double exact = 7.9;
    cout << whole << "\n";
    cout << exact << "\n";
    return 0;
}

Output

7
7.9

Bigger and smaller number types

You will sometimes see these too:

TypeUse it for
long longVery big whole numbers (up to about 9 quintillion)
floatDecimals with less precision than double (uses less memory)
unsigned intWhole numbers that are never negative
#include <iostream>
using namespace std;

int main() {
    int small = 2000000000;
    long long big = 9000000000000;
    cout << small << "\n" << big << "\n";
    return 0;
}

Output

2000000000
9000000000000

Remember: When in doubt: int for whole numbers, double for decimals, string for text.

HOME LABS COMMUNITY DASHBOARD