TExcelTAD
Home › C++ › C++ Data Types

C++ Data Types

Every value in C++ has a type. The type determines how many bytes the value occupies, which range of values it can represent, and which operations make sense. C++ gives you a set of built-in fundamental types plus many library types such as std::string.

Fundamental types

Type Typical size Stores
bool 1 byte true or false
char 1 byte A single character / small integer
short 2 bytes Small whole numbers
int 4 bytes Whole numbers (about ±2.1 billion)
long long 8 bytes Large whole numbers (about ±9.2 × 10¹⁸)
float 4 bytes Decimal numbers, ~7 significant digits
double 8 bytes Decimal numbers, ~15 significant digits

The exact sizes depend on the platform; the standard only guarantees minimums. The sizeof operator tells you the size in bytes on the machine you are using:

cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    cout << "bool:      " << sizeof(bool) << "\n";
    cout << "char:      " << sizeof(char) << "\n";
    cout << "int:       " << sizeof(int) << "\n";
    cout << "long long: " << sizeof(long long) << "\n";
    cout << "float:     " << sizeof(float) << "\n";
    cout << "double:    " << sizeof(double) << "\n";
}
bool:      1
char:      1
int:       4
long long: 8
float:     4
double:    8

Integer types and unsigned

Integer types can be signed (default, allow negative numbers) or unsigned (only zero and positive, but twice the positive range). When an unsigned value goes below zero it wraps around to a huge number — a common source of bugs:

cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    unsigned int apples = 2;
    apples = apples - 3;
    cout << apples << "\n";

    long long stars = 100000000000LL;  // LL suffix = long long literal
    cout << stars << "\n";
}
4294967295
100000000000

Tip: For exact sizes, include <cstdint> and use std::int32_t, std::uint8_t, std::int64_t and friends.

Floating-point types

float and double store numbers with a fractional part. Prefer double — it is more precise and just as fast on modern hardware. You can write numbers in scientific notation with e:

cpp · try it yourself
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    float f = 1.0f / 3.0f;
    double d = 1.0 / 3.0;
    cout << setprecision(17);
    cout << "float:  " << f << "\n";
    cout << "double: " << d << "\n";

    double lightSpeed = 3e8;   // 3 * 10^8
    cout << setprecision(6) << lightSpeed << "\n";
}
float:  0.3333333432674408
double: 0.33333333333333331
3e+08

Floating-point numbers are approximations, so never compare them for exact equality after calculations — check whether the difference is tiny instead.

Characters

A char stores one character in single quotes. Under the hood it is a small integer — the character's code (ASCII for basic Latin letters):

cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    char letter = 'A';
    cout << letter << "\n";
    cout << int(letter) << "\n";      // its numeric code
    char next = letter + 1;
    cout << next << "\n";
}
A
65
B

Type conversion

C++ converts between numeric types automatically when needed (an int becomes a double in 3 + 0.5). Going the other way truncates: int n = 9.99; stores 9. To make a conversion explicit and visible, use static_cast:

cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    int total = 7, count = 2;
    cout << total / count << "\n";                       // integer division
    cout << static_cast<double>(total) / count << "\n";  // real division
    double price = 9.99;
    cout << static_cast<int>(price) << "\n";             // fraction dropped
}
3
3.5
9

auto

With auto, the compiler deduces the type from the initial value: auto n = 5; is an int, auto x = 2.5; is a double. It saves typing for long type names, which you will appreciate when working with the STL.

Test yourself

What does sizeof(int) usually return on modern desktop systems?

  1. 1
  2. 2
  3. 4
  4. 8

What is the result of 7 / 2 when both operands are int?

  1. 3.5
  2. 3
  3. 4
  4. Compile error

Which is the recommended way to convert a double to int explicitly?

  1. (int) d
  2. int(d) only
  3. static_cast<int>(d)
  4. convert<int>(d)