Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 171 Β· Variables and Types

Fundamental Data Types

Learn the C++ fundamental types (bool, char, int, double, void), how big they really are with sizeof, fixed-width types from <cstdint> and numeric_limits.

Every value in a C++ program has a type. The type tells the compiler how many bytes the value occupies, how to interpret those bytes, and which operations make sense. The bit pattern 01000001 is the number 65 if you treat it as an integer, the letter A if you treat it as a character, and part of something else entirely inside a floating-point number. The type is what decides.

C++ is a statically typed language: every variable gets its type when you declare it, and that type never changes. This is one reason C++ is fast. The compiler knows exactly how big everything is and which machine instructions to use, so nothing has to be looked up while the program runs.

This lesson is a map of the fundamental types (the ones built into the language), how big they are, and how to find out their limits. The next few lessons zoom in on integers, floating-point numbers and characters and booleans.

The fundamental types at a glance

Here is the whole family. You will use the first column every day; the rest exist for special jobs.

Group Types Typical use
Boolean bool yes/no values: true or false
Characters char, signed char, unsigned char, wchar_t, char8_t, char16_t, char32_t text and raw bytes
Signed integers short, int, long, long long whole numbers that can be negative
Unsigned integers unsigned short, unsigned int, unsigned long, unsigned long long whole numbers that are never negative, bit patterns
Floating point float, double, long double numbers with a fractional part
No value void "nothing": functions that return nothing
Null pointer std::nullptr_t the type of nullptr

Integers, characters and bool together are called integral types. Integral and floating-point types together are the arithmetic types. Everything else you will meet later, such as std::string, std::vector or your own classes, is built out of these.

A few spelling notes, because you will see all of these in real code:

  • short, long and long long are short for short int, long int and long long int. The shorter spelling is the normal one.
  • unsigned on its own means unsigned int.
  • signed int is just int. The keyword signed only matters for signed char, which is a different type from plain char.

Here they all are in one small program:

C++ One variable of each common type

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Notice the small details: a char holds one character in single quotes, the f after 0.75 makes it a float literal, and the apostrophes in 8'100'000'000 are digit separators (C++14) that make big numbers readable. The value of population does not fit in a 32-bit int, which is why it is a long long.

How big is each type? sizeof

The sizeof operator tells you how many bytes a type or a value occupies. It is evaluated at compile time, so it costs nothing when the program runs. Its result has type std::size_t, an unsigned integer type you will see whenever C++ talks about sizes.

C++ Sizes on our server

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

You can apply sizeof to a type (parentheses required: sizeof(int)) or to an expression (parentheses optional: sizeof measurement).

On our server (64-bit Linux, GCC) you get int = 4, long = 8 and long double = 16. Compile the same program with Microsoft's compiler on 64-bit Windows and long is 4 bytes, long double is 8, and wchar_t is 2. Neither result is wrong.

What the standard actually guarantees

C++ runs on everything from 8-bit microcontrollers to supercomputers, so the standard deliberately does not fix most sizes. Instead it sets minimums and an ordering:

Type Guaranteed at least Common on 64-bit desktops
char 8 bits, and sizeof(char) is always exactly 1 8 bits
short 16 bits 16 bits
int 16 bits 32 bits
long 32 bits 64 bits on Linux/macOS, 32 bits on Windows
long long 64 bits 64 bits

The ordering rule is sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long). Sizes like these are called implementation-defined: each compiler and platform picks values and documents them.

Two consequences matter in practice:

  • A "byte" in C++ is a char, whatever size that is. sizeof counts in chars. On every mainstream platform a byte is 8 bits, and the exact number is available as CHAR_BIT from <climits>.
  • Don't assume long is 64 bits. Code that stores file sizes or timestamps in long works on Linux and silently breaks on Windows. If you need a specific width, say so with a fixed-width type (next section).

Since C++20 the standard also requires signed integers to use two's complement representation, which every real machine already did. That settles how negative numbers are stored, but it does not make signed overflow safe; the Integer Types lesson explains why.

Fixed-width integers from <cstdint>

When the exact number of bits matters (file formats, network protocols, hashing, hardware registers), include <cstdint> and use the fixed-width aliases:

Type Meaning
std::int8_t, std::int16_t, std::int32_t, std::int64_t signed, exactly 8/16/32/64 bits
std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t unsigned, exactly 8/16/32/64 bits
std::int_least32_t, std::int_fast32_t, … at least 32 bits / the fastest type with at least 32 bits
std::intptr_t, std::uintptr_t big enough to hold a pointer value
std::size_t (from <cstddef>) sizes and counts; what sizeof returns

These are not new types. They are type aliases for one of the fundamental types; on our server std::int64_t is long and std::int32_t is int. The exact-width ones are technically optional (a platform without an 8-bit type can't provide std::int8_t), but every desktop, phone and server compiler has them.

There is one trap that catches almost everybody. std::int8_t and std::uint8_t are almost always aliases for signed char and unsigned char, so streams print them as characters:

C++ The int8_t surprise

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

std::println (and std::format) print signed char and unsigned char as integers, while std::cout prints them as characters, so you see 65 66 from one and A B from the other. If you use streams with 8-bit integers, convert with static_cast<int> before printing. Type Conversions and Casts covers static_cast properly.

Which integer should you use? Use int for ordinary counters and arithmetic, std::size_t for sizes and indexes that come from containers, std::int64_t (or long long) when values can exceed about two billion, and the exact-width types when the bit count is part of the contract. Don't reach for short to "save memory" on single variables: it saves nothing and every arithmetic operation converts it to int anyway.

Finding the limits: std::numeric_limits

How large can an int get? Don't memorize it: ask the compiler. The <limits> header provides std::numeric_limits<T>, a template that describes any arithmetic type T:

C++ Asking a type about itself

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The output shows that a 32-bit int runs from βˆ’2,147,483,648 to 2,147,483,647, about Β±2.1 billion. That is smaller than people expect: the world population, the number of milliseconds in a month, or the byte size of a large file all go past it.

Watch out for one naming oddity. For integers, min() is the most negative value. For floating-point types, min() is the smallest positive normal number (about 2.2e-308 for double). The most negative double is lowest(). Using numeric_limits<double>::min() as the starting value for "find the maximum" is a classic bug.

Other useful members are digits (number of non-sign bits, or mantissa bits for floating point), digits10 (decimal digits that survive a round trip), is_signed, is_integer, epsilon(), infinity() and quiet_NaN(). Older code uses macros such as INT_MAX from <climits>; they hold the same values, but numeric_limits works with any type, including inside templates.

bool, char and floating point in one sentence each

Each of these gets its own lesson, so here is just enough to get going:

  • bool holds true or false. Numbers and pointers convert to bool (zero and null are false, everything else true), and bool converts to 1 or 0.
  • char is a one-byte integer that usually stores a character code. 'A' is the number 65 in ASCII. Whether plain char is signed or unsigned depends on the platform.
  • float (usually 32 bits, about 7 significant digits) and double (usually 64 bits, about 15–16 digits) store approximate real numbers. Use double by default.

void: the type with no values

void is the odd one out: there are no void values, and you can't declare a void variable. It appears in three places:

C++
void log_message(const char* text);   // the function returns nothing

int read_sensor(void);                // C style for "no parameters"; in C++ just write ()

void* raw_memory = nullptr;           // pointer to "something", type unknown

You will use void return types constantly. The (void) parameter list is a leftover from C; in C++ an empty () already means "no parameters". void* pointers belong to low-level code; modern C++ rarely needs them (see Pointers).

Choosing a type: a quick guide

You are storing… Use
a yes/no flag bool
a count, a loop counter, a small whole number int
a container size or an index into a container std::size_t
a whole number that may exceed Β±2 billion std::int64_t or long long
an exact bit width (protocols, files, hashing) std::uint32_t, std::uint8_t, …
a measurement or calculation with fractions double
a single character char
text std::string (see Strings with std::string)
money an integer count of cents, never float or double

The last row deserves emphasis. Floating-point types can't store most decimal fractions exactly, so 0.10 dollars becomes a tiny bit more or less than ten cents. Count cents in an integer instead. Floating-Point Numbers shows why.

Check your understanding

Quick check Β· 5 questions

of 5 answered You scored / 5 Β· perfect!

  1. 1. Which statement about int does the C++ standard guarantee?

    The standard only sets minimums and an ordering. int must be at least 16 bits and sizeof(int) <= sizeof(long). It happens to be 32 bits on all mainstream desktop platforms.
  2. 2. You need a value that is exactly 32 bits on every platform, for a binary file header. Which type fits best?

    The fixed-width aliases from <cstdint> state the exact bit count. long is 32 bits on Windows but 64 on Linux, and int is only guaranteed 16 bits.
  3. 3. What does this print? std::int8_t n = 65; std::cout << n;

    std::int8_t is almost always signed char, and streams print character types as characters. Code 65 is A. Use static_cast<int>(n) (or std::println) to see the number.
  4. 4. What is std::numeric_limits<double>::min()?

    For floating-point types, min() is the smallest positive normal value (about 2.2e-308). The most negative value is lowest().
  5. 5. Which of these can you NOT do with void?

    void has no values, so a void variable is a compile error. It is fine as a return type and as the pointee type of void*.

Summary

  • C++ is statically typed: the type decides size, representation and allowed operations, and it never changes.
  • Fundamental types: bool, the character types, signed and unsigned integers, float/double/long double, void and std::nullptr_t.
  • sizeof gives sizes in bytes at compile time. Most sizes are implementation-defined; the standard guarantees minimums (int β‰₯ 16 bits, long β‰₯ 32, long long β‰₯ 64) and an ordering.
  • long is 64 bits on Linux/macOS but 32 on Windows, so use <cstdint> types like std::int64_t or std::uint32_t when the width matters.
  • std::int8_t/std::uint8_t print as characters with std::cout; convert to int first.
  • std::numeric_limits<T> reports min(), max(), lowest(), digits and more. For floating point, min() is the smallest positive value.
  • Defaults: int for counting, std::size_t for sizes, double for fractions, integer cents for money.

Next: Integer Types, where you'll learn signed vs unsigned, what happens on overflow, and how to write integer literals.