Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

C++  Module 2 – Types, values and expressions

C++ fundamental types: integers, floats, char, bool

What each C++ fundamental type holds, why long is 8 bytes on macOS and Linux but 4 on Windows, and how to pick exact widths with <cstdint>.

  • Beginner
  • 10 minutes
  • Examples run with Clang 21.0.0, GCC 16.2.0 and Python 3.14.8
  • By MySmartCoPilot
Free lesson, Pro answers: 10 minutes
Free: the whole lesson. With a pass: the model answers to its 2 interview questions.

What you will learn

  • Choose an integer type by the range of values it must hold
  • Use the fixed-width types from <cstdint> when sizes matter
  • Query sizes and limits with sizeof and std::numeric_limits
  • Pick unsigned char or std::byte for raw bytes, since the sign of plain char differs by platform
  • Predict how bool converts to and from numbers

Before you start

What you’ll learn · Before you start

What you will learn

  • Choose an integer type by the range of values it must hold
  • Use the fixed-width types from <cstdint> when sizes matter
  • Query sizes and limits with sizeof and std::numeric_limits
  • Pick unsigned char or std::byte for raw bytes, since the sign of plain char differs by platform
  • Predict how bool converts to and from numbers

Before you start

On this page

A long is 8 bytes on a 64-bit Mac or Linux machine and only 4 bytes on 64-bit Windows, while an int is 4 bytes on all three. The fundamental types are the ones built into the C++ language: the integers (short, int, long and long long, each signed or unsigned), the floating-point types (float, double and long double), the character types (char and its relatives), bool, and void. The standard does not fix their sizes. It fixes a minimum width for each integer type and the order between them, and the rest comes from the platform. Even the sign of plain char differs between platforms.

So choose a type by the range of values it must hold, ask the compiler with sizeof and std::numeric_limits when you need the facts, and use the exact-width types of <cstdint>, such as std::int64_t, wherever a size is part of the contract.

The platform, not the standard, decides each type’s size

sizeof(T) is the size of a type in bytes, where a byte is a char, 8 bits on every platform you are likely to use. This program prints it for every fundamental type:

sizes.cpp C++ · sizes.cpp
#include <cstddef>
#include <print>

template <class T>
void show(const char* name) {
    std::println("{:<12} {:>2} bytes  {:>3} bits", name, sizeof(T), 8 * sizeof(T));
}

int main() {
    show<bool>("bool");
    show<char>("char");
    show<short>("short");
    show<int>("int");
    show<long>("long");
    show<long long>("long long");
    show<float>("float");
    show<double>("double");
    show<long double>("long double");
    show<wchar_t>("wchar_t");
    show<char8_t>("char8_t");
    show<char16_t>("char16_t");
    show<char32_t>("char32_t");
    show<void*>("void*");
    show<std::size_t>("std::size_t");
}
Sizes on a 64-bit Mac, with both compilers Python · sizes_session.py
from session import run

run("""
clang++ -std=c++23 -Wall -Wextra sizes.cpp -o sizes
./sizes
g++-16 -std=c++23 -Wall -Wextra sizes.cpp -o sizes
./sizes
""")

Output

$ clang++ -std=c++23 -Wall -Wextra sizes.cpp -o sizes
$ ./sizes
bool          1 bytes    8 bits
char          1 bytes    8 bits
short         2 bytes   16 bits
int           4 bytes   32 bits
long          8 bytes   64 bits
long long     8 bytes   64 bits
float         4 bytes   32 bits
double        8 bytes   64 bits
long double   8 bytes   64 bits
wchar_t       4 bytes   32 bits
char8_t       1 bytes    8 bits
char16_t      2 bytes   16 bits
char32_t      4 bytes   32 bits
void*         8 bytes   64 bits
std::size_t   8 bytes   64 bits
$ g++-16 -std=c++23 -Wall -Wextra sizes.cpp -o sizes
$ ./sizes
bool          1 bytes    8 bits
char          1 bytes    8 bits
short         2 bytes   16 bits
int           4 bytes   32 bits
long          8 bytes   64 bits
long long     8 bytes   64 bits
float         4 bytes   32 bits
double        8 bytes   64 bits
long double   8 bytes   64 bits
wchar_t       4 bytes   32 bits
char8_t       1 bytes    8 bits
char16_t      2 bytes   16 bits
char32_t      4 bytes   32 bits
void*         8 bytes   64 bits
std::size_t   8 bytes   64 bits

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 sizes_session.py

As in every session of this track, the lines that start with $ are commands, and the rest is what they printed. Both compilers agree, because the sizes belong to the platform rather than to the compiler: the operating system’s data model: The sizes a platform gives int, long, long long and pointers. 64-bit macOS and Linux use LP64, where long and pointers are 64 bits; 64-bit Windows uses LLP64, where long stays 32 bits. and the processor’s calling convention decide them, and every compiler for that platform follows. 64-bit macOS and Linux use a data model called LP64, in which long and pointers are 64 bits. 64-bit Windows uses LLP64, in which only long long and pointers are, and Microsoft’s documentation lists long as 4 bytes there. The chart sets the two data models side by side, type by type:

Bytes per type on 64-bit macOS and Linux against 64-bit Windows: long and wchar_t differ, short, int, long long and pointers do not.Bytes per type on 64-bit systemsmacOS and Linux (LP64)Windows (LLP64)short22int44long84long long88void*88wchar_t42

Sizes of the integer types on 64-bit systems

Text description of the diagram

A bar chart with two bars per type, one for 64-bit macOS and Linux (the LP64 data model) and one for 64-bit Windows (the LLP64 data model). Each bar is labelled with its size in bytes.

  • short: 2 bytes on both.
  • int: 4 bytes on both.
  • long: 8 bytes on macOS and Linux, 4 bytes on Windows.
  • long long: 8 bytes on both.
  • void*, a pointer: 8 bytes on both.
  • wchar_t: 4 bytes on macOS and Linux, 2 bytes on Windows.

The standard itself promises much less. Since C++20 it sets these minimum widths, and requires each type to be at least as wide as the one before it. The Mac column comes from the run above, the Windows column from Microsoft’s documentation:

Type Standard minimum 64-bit Mac 64-bit Windows
signed char 8 bits 1 byte 1 byte
short 16 bits 2 bytes 2 bytes
int 16 bits 4 bytes 4 bytes
long 32 bits 8 bytes 4 bytes
long long 64 bits 8 bytes 8 bytes

An int of 16 bits is allowed, and some small embedded processors have one. The safe reading of the table is that int holds at least -32,768 to 32,767, long at least about plus or minus 2.1 billion, and long long at least about plus or minus 9.2 × 10¹⁸. Anything more is a property of your platform.

Key idea

The standard promises minimum widths and an order; the platform’s data model sets the sizes, so a long is 8 bytes on 64-bit macOS and Linux and 4 bytes on 64-bit Windows.

Checkpoint

Check: Sizes and data models

  1. Question 1 of 2 A program keeps a file size of 3,000,000,000 bytes in a long. On which 64-bit systems does that value fit?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: On macOS and Linux, but not on Windows

    long is 8 bytes on 64-bit macOS and Linux (LP64) and 4 bytes on 64-bit Windows (LLP64). For a value that can go past 2 billion, long long or std::int64_t fits on all of them.

  2. Question 2 of 2 What does the name LP64 say about 64-bit macOS and Linux?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: long and pointers are 64 bits

    A data model is named after the types that are 64 bits wide. In LP64 those are long and pointers; in LLP64, the model of 64-bit Windows, they are long long and pointers.

N bits give a fixed range, and <limits> reports it

<limits> describes each arithmetic type: its lowest and largest values, how many bits or decimal digits it keeps, whether it is signed. It works at compile time, so it costs nothing at run time:

limits.cpp C++ · limits.cpp
#include <limits>
#include <print>

template <class T>
void row(const char* name) {
    using limits = std::numeric_limits<T>;
    std::println("{:<18} {:>26} {:>26}", name, limits::lowest(), limits::max());
}

int main() {
    std::println("{:<18} {:>26} {:>26}", "type", "lowest", "max");
    row<signed char>("signed char");
    row<unsigned char>("unsigned char");
    row<short>("short");
    row<int>("int");
    row<unsigned>("unsigned");
    row<long long>("long long");
    row<unsigned long long>("unsigned long long");
    row<float>("float");
    row<double>("double");
    std::println("");
    std::println("decimal digits a float keeps: {}, a double: {}", std::numeric_limits<float>::digits10,
                 std::numeric_limits<double>::digits10);
}
Lowest and largest values Python · limits_session.py
from session import run

run("""
clang++ -std=c++23 -Wall -Wextra limits.cpp -o limits
./limits
g++-16 -std=c++23 -Wall -Wextra limits.cpp -o limits
./limits
""")

Output

$ clang++ -std=c++23 -Wall -Wextra limits.cpp -o limits
$ ./limits
type                                   lowest                        max
signed char                              -128                        127
unsigned char                               0                        255
short                                  -32768                      32767
int                               -2147483648                 2147483647
unsigned                                    0                 4294967295
long long                -9223372036854775808        9223372036854775807
unsigned long long                          0       18446744073709551615
float                          -3.4028235e+38              3.4028235e+38
double               -1.7976931348623157e+308    1.7976931348623157e+308

decimal digits a float keeps: 6, a double: 15
$ g++-16 -std=c++23 -Wall -Wextra limits.cpp -o limits
$ ./limits
type                                   lowest                        max
signed char                              -128                        127
unsigned char                               0                        255
short                                  -32768                      32767
int                               -2147483648                 2147483647
unsigned                                    0                 4294967295
long long                -9223372036854775808        9223372036854775807
unsigned long long                          0       18446744073709551615
float                          -3.4028235e+38              3.4028235e+38
double               -1.7976931348623157e+308    1.7976931348623157e+308

decimal digits a float keeps: 6, a double: 15

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 limits_session.py

Every signed integer runs from -2ᴺ⁻¹ to 2ᴺ⁻¹ - 1 for its N bits, and every unsigned one from 0 to 2ᴺ - 1. That is guaranteed since C++20, which made two's complement: The way signed integers are stored, the only one C++20 allows: the top bit counts as minus 2 to the power N − 1, so N bits hold −2ᴺ⁻¹ to 2ᴺ⁻¹ − 1, and the bits 1111 1111 of a signed byte are −1. the only allowed representation of signed integers; earlier standards also allowed two others that mainstream processors no longer use. What two’s complement did not change is overflow: going past max() with a signed type is still undefined behaviour, the subject of the next lesson.

Step through one byte to see where both ranges come from, bit by bit:

Interactive · step by step One byte, read as unsigned char and as signed char Step through five bit patterns of one byte. The box beside each pattern gives the value it stands for in each type.
One byte, read as unsigned char and as signed char, step 6 of 6. Showing 0000 0000, unsigned 0, 0111 1111, unsigned 127, 1000 0000, unsigned 128, 1100 1000 (0xC8), unsigned 200, 1111 1111, unsigned 255.0000 0000unsigned 0signed 00111 1111unsigned 127signed 1271000 0000unsigned 128signed −1281100 1000 (0xC8)unsigned 200signed −561111 1111unsigned 255signed −1

The drawing shows the whole scene; its 6 steps are listed below.

Every step as text
  1. Zero. A byte has 8 bits, each worth a power of two: 128, 64, 32, 16, 8, 4, 2 and 1. All zeros is 0, read as unsigned char or as signed char.
  2. The largest positive. 0111 1111 is 127 either way: 64 + 32 + 16 + 8 + 4 + 2 + 1. It is the largest signed char, 2⁷ − 1.
  3. The top bit. With the top bit set the two readings part. In unsigned char it is worth +128. In two's complement, which C++20 requires, it is worth −128, so 1000 0000 is the lowest signed char, −2⁷.
  4. The byte 0xC8. 1100 1000 is the byte 0xC8 that the section on character types reads: 128 + 64 + 8 = 200 as unsigned, and −128 + 64 + 8 = −56 as signed.
  5. All ones. 1111 1111 is 255 as unsigned, 2⁸ − 1, and −1 as signed. So unsigned char runs from 0 to 255 and signed char from −128 to 127, as the limits session printed.
  6. Any width. The same holds for every width of N bits: unsigned from 0 to 2ᴺ − 1, signed from −2ᴺ⁻¹ to 2ᴺ⁻¹ − 1. For a 32-bit int that is −2,147,483,648 to 2,147,483,647.

Two details of the output are worth a second look. For floating-point types, lowest() is the most negative value, while min() means something else: the smallest positive normal number, about 1.2 × 10⁻³⁸ for a float. And digits10 says how many decimal digits a type keeps without fail: 6 for float and 15 for double. The floating-point lesson takes these apart.

Checkpoint

Check: Ranges and std::numeric_limits

  1. Question 1 of 2 What is the largest value of a 16-bit unsigned type, such as std::uint16_t?

    Type a number.

    A hint

    An unsigned type of N bits reaches 2ᴺ − 1.

    Show the answer: Question 1 of 2

    Answer: 65535

    2¹⁶ − 1 = 65,535. That is why 40,000 fits std::uint16_t but not std::int16_t, whose largest value is 32,767.

  2. Question 2 of 2 Which member gives the most negative value a double can hold?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: std::numeric_limits<double>::lowest()

    For integer types min() and lowest() are the same. For floating-point types they differ: lowest() is the most negative value and min() the smallest positive normal one.

Use <cstdint> when a size is part of a contract

When a size is part of a contract, such as a file format, a network protocol or an interface to hardware, use the fixed-width integer type: An integer type of <cstdint> whose name gives its exact width, such as std::int32_t or std::uint8_t: exactly that many bits, with no padding bits, on every platform that provides it. of <cstdint>: std::int8_t, std::int16_t, std::int32_t and std::int64_t, and the unsigned std::uint8_t to std::uint64_t. Each has exactly the width in its name, with no padding bits. std::size_t, the type sizeof returns and the standard containers use for sizes, is unsigned, and on mainstream platforms as wide as a pointer: 8 bytes here.

Fixed-width types and the smallest type for a value

fixed_width.cpp

#include <cstddef>
#include <cstdint>
#include <iostream>
#include <print>
#include <type_traits>

int main() {
    std::int32_t ms_per_day{86'400'000};          // fits: at most 2,147,483,647
    std::int64_t ms_per_year{31'536'000'000};     // needs more than 32 bits
    std::uint8_t grade{66};                       // one byte, 0 to 255
    std::println("{} ms a day, {} ms a year", ms_per_day, ms_per_year);

    // std::uint8_t is a character type underneath, and streams print characters:
    std::cout << "cout prints grade as " << grade << '\n';
    std::println("println prints grade as {}", grade);

    static_assert(sizeof(std::int64_t) == 8);
    static_assert(std::is_unsigned_v<std::size_t>);
    static_assert(std::is_same_v<std::uint8_t, unsigned char>);
}

choose_types.cpp

#include <cstdint>
#include <print>
#include <utility>

// The smallest fixed-width signed type that can hold value.
constexpr const char* smallest_signed(long long value) {
    if (std::in_range<std::int8_t>(value)) return "std::int8_t";
    if (std::in_range<std::int16_t>(value)) return "std::int16_t";
    if (std::in_range<std::int32_t>(value)) return "std::int32_t";
    return "std::int64_t";
}

int main() {
    for (long long value : {100LL, -200LL, 40'000LL, 86'400'000LL, 3'000'000'000LL, 31'536'000'000LL}) {
        std::println("{:>14} needs {}", value, smallest_signed(value));
    }
    // The same checks while the program compiles: a wrong choice stops the build.
    static_assert(std::in_range<std::int16_t>(-200));
    static_assert(!std::in_range<std::int16_t>(40'000));
    static_assert(std::in_range<std::uint16_t>(40'000));
    static_assert(!std::in_range<std::int32_t>(3'000'000'000LL));
    static_assert(std::in_range<std::uint32_t>(3'000'000'000LL));
}
Both files on both compilers Python · fixed_width_session.py
from session import run

run("""
clang++ -std=c++23 -Wall -Wextra fixed_width.cpp -o fixed_width
./fixed_width
clang++ -std=c++23 -Wall -Wextra choose_types.cpp -o choose_types
./choose_types
g++-16 -std=c++23 -Wall -Wextra fixed_width.cpp -o fixed_width
./fixed_width
g++-16 -std=c++23 -Wall -Wextra choose_types.cpp -o choose_types
./choose_types
""")

Output

$ clang++ -std=c++23 -Wall -Wextra fixed_width.cpp -o fixed_width
$ ./fixed_width
86400000 ms a day, 31536000000 ms a year
cout prints grade as B
println prints grade as 66
$ clang++ -std=c++23 -Wall -Wextra choose_types.cpp -o choose_types
$ ./choose_types
           100 needs std::int8_t
          -200 needs std::int16_t
         40000 needs std::int32_t
      86400000 needs std::int32_t
    3000000000 needs std::int64_t
   31536000000 needs std::int64_t
$ g++-16 -std=c++23 -Wall -Wextra fixed_width.cpp -o fixed_width
$ ./fixed_width
86400000 ms a day, 31536000000 ms a year
cout prints grade as B
println prints grade as 66
$ g++-16 -std=c++23 -Wall -Wextra choose_types.cpp -o choose_types
$ ./choose_types
           100 needs std::int8_t
          -200 needs std::int16_t
         40000 needs std::int32_t
      86400000 needs std::int32_t
    3000000000 needs std::int64_t
   31536000000 needs std::int64_t

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 fixed_width_session.py

The first file shows the classic surprise of the 8-bit types. On these platforms std::uint8_t is another name for unsigned char, and std::cout prints a char type as a character, so the grade 66 comes out as the letter B. std::println formats signed char and unsigned char as numbers, which is one more reason to prefer it. With std::cout, write +grade: the unary plus promotes the value to int.

The second file answers the question behind every choice of type, “which type holds this value?”, with std::in_range from <utility> (C++20). It is true when a value fits the type given in angle brackets, whatever the signs of the two types, and it works at compile time, so the static_assert lines at the end turn a wrong choice into a build error. 86,400,000 milliseconds, one day, needs 32 bits; 31,536,000,000, one year of 365 days, needs 64. The numbers are ordinary: a counter of milliseconds or of bytes outgrows std::int32_t sooner than people expect. Step through the program’s tests with each value of its loop:

Interactive · stage by stage The smallest fixed-width signed type for each value Step through smallest_signed() with the six values of the loop in choose_types.cpp. Each value goes down the tests until one says it fits.
The smallest fixed-width signed type for each value, step 7 of 7. Showing The value (31,536,000,000), Fits std::int8_t?, Fits std::int16_t?, Fits std::int32_t?, std::int64_t. In focus: Fits std::int8_t?, Fits std::int16_t?, Fits std::int32_t?.The value31,536,000,000Fits std::int8_t?−128 to 127Fits std::int16_t?−32,768 to 32,767Fits std::int32_t?about ±2.1 billionstd::int64_tabout ±9.2 × 10¹⁸

The drawing shows the whole scene; its 7 steps are listed below.

Every step as text
  1. 100. 100 fits std::int8_t, whose range is −128 to 127, so the first test answers: std::int8_t.
  2. −200. −200 is below −128, so it goes on to the next test, and it fits std::int16_t.
  3. 40,000. 40,000 is above 32,767, the largest std::int16_t, so it needs std::int32_t. It would fit std::uint16_t, 0 to 65,535, as a static_assert in the file checks.
  4. 86,400,000. 86,400,000, the milliseconds in a day, fits std::int32_t, whose largest value is 2,147,483,647.
  5. 3,000,000,000. 3,000,000,000 is above 2,147,483,647, so no 32-bit signed type holds it, and the answer is std::int64_t. std::uint32_t would hold it, as another static_assert checks.
  6. 31,536,000,000. 31,536,000,000, the milliseconds in a year of 365 days, needs std::int64_t as well.
  7. While compiling. std::in_range works at compile time, so the static_assert lines run the same kind of test while the file compiles: a type too small for its value stops the build.

For everyday work these rules are enough:

  • int for counts and indexes that stay well below 2 billion, which is what most code does.
  • long long or std::int64_t for anything that can reach 2 billion: file sizes, money in the smallest unit, timestamps in milliseconds. Avoid plain long in portable code, because its size depends on the platform.
  • The fixed-width types for data that leaves the program, so that every machine reads the same bytes.
  • Signed types for arithmetic, as rule ES.102 of the C++ Core Guidelines advises. std::size_t appears whenever you ask a container for its size, and mixing it with signed values has traps of its own, covered in the next lesson.

Checkpoint

Check: The fixed-width types of <cstdint>

  1. Question 1 of 2 What does this print?

    Read the code, then choose one answer.

    std::uint8_t level{65};
    std::cout << level << ' ' << +level << '\n';
    Show the answer: Question 1 of 2

    Answer: A 65

    std::uint8_t is another name for unsigned char on these platforms, so std::cout prints it as a character, as the grade 66 came out as B in fixed_width.cpp. Write +level, or use std::println, to print the number.

  2. Question 2 of 2 Which type would smallest_signed() return for 2,200,000,000?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: std::int64_t

    The function tests std::int8_t, std::int16_t and std::int32_t in that order with std::in_range and falls back to std::int64_t, as it did for 3,000,000,000.

Three character types, not two

char, signed char and unsigned char are three distinct types, even though plain char behaves exactly like one of the other two. Which one is up to the platform:

char_sign.cpp C++ · char_sign.cpp
#include <climits>
#include <print>
#include <type_traits>

int main() {
    char byte = static_cast<char>(0xC8);   // the bit pattern 11001000
    std::println("plain char is signed: {}", std::is_signed_v<char>);
    std::println("range of char: {} to {}", CHAR_MIN, CHAR_MAX);
    std::println("the byte 0xC8 as an int: {}", static_cast<int>(byte));
    std::println("char is the same type as signed char: {}", std::is_same_v<char, signed char>);
}
Plain char on macOS, and as Linux on arm64 sees it Python · char_sign_session.py
from session import run

run("""
clang++ -std=c++23 -Wall -Wextra char_sign.cpp -o char_sign
./char_sign
g++-16 -std=c++23 -Wall -Wextra char_sign.cpp -o char_sign
./char_sign
# The same file as compilers build it for Linux on arm64, where plain char is unsigned:
g++-16 -std=c++23 -Wall -Wextra -funsigned-char char_sign.cpp -o char_sign
./char_sign
""")

Output

$ clang++ -std=c++23 -Wall -Wextra char_sign.cpp -o char_sign
$ ./char_sign
plain char is signed: true
range of char: -128 to 127
the byte 0xC8 as an int: -56
char is the same type as signed char: false
$ g++-16 -std=c++23 -Wall -Wextra char_sign.cpp -o char_sign
$ ./char_sign
plain char is signed: true
range of char: -128 to 127
the byte 0xC8 as an int: -56
char is the same type as signed char: false
# The same file as compilers build it for Linux on arm64, where plain char is unsigned:
$ g++-16 -std=c++23 -Wall -Wextra -funsigned-char char_sign.cpp -o char_sign
$ ./char_sign
plain char is signed: false
range of char: 0 to 255
the byte 0xC8 as an int: 200
char is the same type as signed char: false

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 char_sign_session.py

On Apple silicon, plain char is signed: Apple’s documentation lists it as one of the places where Apple’s 64-bit ABI differs from Arm’s standard one. The byte 0xC8 therefore reads as -56. Arm’s own procedure call standard, AAPCS64, which Linux on arm64 follows, makes plain char unsigned. GCC’s -funsigned-char builds the same file that way, and the byte reads as 200. On Windows, Microsoft’s compiler makes char signed unless you pass /J. Code that works on raw bytes, such as a checksum or a file parser, should therefore say what it means: unsigned char, std::uint8_t or std::byte, never plain char. A whole program, its libraries included, must be built with one setting, so treat -funsigned-char as a way to test, not as a fix.

The other character types hold units of text encodings. char8_t, char16_t and char32_t hold UTF-8, UTF-16 and UTF-32 code units, at 1, 2 and 4 bytes everywhere, as the sizes session showed. wchar_t is the odd one: 4 bytes on macOS and Linux, 2 bytes on Windows, so it holds different encodings on different systems. A later lesson of this module is about text encodings.

Checkpoint

Check: The sign of plain char

  1. Question 1 of 2 char_sign.cpp is built for Linux on arm64, where plain char follows AAPCS64. What does static_cast<int>(byte) give for the byte 0xC8?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: 200

    The same bit pattern, 1100 1000, is 200 when char is unsigned and -56 when it is signed. Code that reads bytes says which it means with unsigned char, std::uint8_t or std::byte.

  2. Question 2 of 2 On Apple silicon plain char behaves like signed char. Is it the same type?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: No: char, signed char and unsigned char are three distinct types

    The standard makes char a type of its own whose behaviour copies one of the other two character types. That is why char, signed char and unsigned char are three types, not two.

bool converts to and from numbers: zero is false, the rest true

bool holds true or false and takes 1 byte on these platforms. It converts both ways with numbers:

bools.cpp C++ · bools.cpp
#include <iostream>
#include <print>

int main() {
    bool from_negative = -3;      // any value other than zero converts to true
    bool from_zero = 0.0;         // zero converts to false
    int from_true = true;         // true converts to 1, false to 0
    std::println("{} {} {}", from_negative, from_zero, from_true);
    std::println("true + true = {}", true + true);
    std::cout << "cout prints true as " << true << ", with std::boolalpha as " << std::boolalpha << true << '\n';
}
Conversions to and from bool Python · bools_session.py
from session import run

run("""
clang++ -std=c++23 -Wall -Wextra bools.cpp -o bools
./bools
g++-16 -std=c++23 -Wall -Wextra bools.cpp -o bools
./bools
""")

Output

$ clang++ -std=c++23 -Wall -Wextra bools.cpp -o bools
$ ./bools
true false 1
true + true = 2
cout prints true as 1, with std::boolalpha as true
$ g++-16 -std=c++23 -Wall -Wextra bools.cpp -o bools
$ ./bools
true false 1
true + true = 2
cout prints true as 1, with std::boolalpha as true

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 bools_session.py

Any number other than zero converts to true, and zero to false; true converts to 1 and false to 0. In arithmetic a bool is promoted to int first, so true + true is 2. std::cout prints a bool as 1 or 0 unless you set std::boolalpha, while std::println prints true and false.

The same promotion sets the classic trap of comparing with true. An int compared with true is compared with 1, so a flag in the C style that holds 2 is not equal to true, although it converts to true:

bool_compare.cpp C++ · bool_compare.cpp
#include <print>

int main() {
    int found = 2;   // a flag in the C style: any value other than 0 means yes
    std::println("found == true:            {}", found == true);   // true is promoted to the int 1
    std::println("found != 0:               {}", found != 0);
    std::println("static_cast<bool>(found): {}", static_cast<bool>(found));
}
A flag of 2, compared three ways Python · bool_compare_session.py
from session import run

run("""
clang++ -std=c++23 -Wall -Wextra bool_compare.cpp -o bool_compare
./bool_compare
g++-16 -std=c++23 -Wall -Wextra bool_compare.cpp -o bool_compare
./bool_compare
""")

Output

$ clang++ -std=c++23 -Wall -Wextra bool_compare.cpp -o bool_compare
$ ./bool_compare
found == true:            false
found != 0:               true
static_cast<bool>(found): true
$ g++-16 -std=c++23 -Wall -Wextra bool_compare.cpp -o bool_compare
$ ./bool_compare
found == true:            false
found != 0:               true
static_cast<bool>(found): true

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 bool_compare_session.py

Comparing an int with true

found == true printed false for int found = 2, because true becomes the int 1 before the comparison. Ask whether a number is non-zero with found != 0, or convert it with static_cast<bool>(found), which both printed true.

Checkpoint

Check: bool and numbers

  1. Question 1 of 2 What does this print?

    Read the code, then choose one answer.

    int ready = 5;
    std::println("{} {}", ready == true, static_cast<bool>(ready));
    Show the answer: Question 1 of 2

    Answer: false true

    In a comparison with an int, true is promoted to 1, so only a value of exactly 1 equals true. Ask whether a number is non-zero with ready != 0, or convert it with static_cast<bool>, as bool_compare.cpp did for 2.

  2. Question 2 of 2 Which line prints the word true?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: std::println("{}", true);

    bools.cpp printed true with std::println, and 1 with std::cout until std::boolalpha was set.

float and double are IEEE 754; long double is not portable

float and double are the IEEE 754 binary formats of 32 and 64 bits on every mainstream platform: a float keeps 6 decimal digits without fail and a double 15, as the limits session printed. Prefer double unless memory is tight, as in large arrays or on graphics hardware. long double is the least portable fundamental type. On Apple silicon it is identical to double, 8 bytes, which Apple documents as another difference from the standard Arm ABI, where it is a 16-byte quad-precision type. The next lessons cover what floating-point numbers can and cannot store exactly.

The sizes and limits sessions above give the three types on this machine:

Type Bytes here Decimal digits it keeps
float 4 6
double 8 15
long double 8 on Apple silicon the same as double here

Checkpoint

Check: float, double and long double

  1. One question A measurement must keep 10 significant decimal digits. Which type keeps them without fail?

    Choose one answer.

    Show the answer: One question

    Answer: double

    digits10 is 6 for float and 15 for double, so 10 digits need a double. Prefer double unless memory is tight.

Practise: types, sizes and limits

Test yourself on sizes, ranges and the character types. The session opens with whichever idea you have answered least well, and explains each choice as you make it:

Practice that adapts to you

Practise this lesson

13 questions about 6 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 13 How many bytes is a long on 64-bit Windows?

    Choose one answer.

    Show the answer: Question 1 of 13

    Answer: 4

    64-bit Windows uses the LLP64 data model, in which int and long are 32 bits and only long long and pointers are 64. Every compiler for the platform follows it. On 64-bit macOS and Linux (LP64) a long is 8 bytes.

  2. Question 2 of 13 Which of these does the C++ standard guarantee?

    Choose every answer that is right.

    Show the answer: Question 2 of 13

    Answer:

    • A long long is at least 64 bits wide
    • sizeof(char) is 1
    • An int is at least 16 bits wide

    The standard fixes minimum widths and defines sizeof in units of char, so sizeof(char) is 1 by definition. Exactly 32 bits for int and 8 bytes for long are properties of particular platforms; 64-bit Windows has a 4-byte long.

  3. Question 3 of 13 What does std::numeric_limits<double>::min() return?

    Choose one answer.

    Show the answer: Question 3 of 13

    Answer: The smallest positive normal double, about 2.2 × 10⁻³⁰⁸

    For floating-point types, min() is the smallest positive normal value; the most negative one is lowest(). For integer types the two are the same. The gap between 1.0 and the next double is epsilon().

  4. Question 4 of 13 What is the largest value of a 16-bit signed integer type, such as short on these platforms?

    Type a number.

    A hint

    A signed type of N bits reaches 2ᴺ⁻¹ − 1.

    Show the answer: Question 4 of 13

    Answer: 32767

    2¹⁵ − 1 = 32,767, as the limits session printed for short. The lowest value is -32,768.

  5. Question 5 of 13 Which header declares std::int64_t?

    Choose one answer.

    Show the answer: Question 5 of 13

    Answer: <cstdint>

    Include <cstdint> for std::int8_t, std::int16_t, std::int32_t and std::int64_t and their unsigned partners.

  6. Question 6 of 13 What does this print?

    Read the code, then choose one answer.

    std::uint8_t grade{66};
    std::cout << grade << '\n';
    Show the answer: Question 6 of 13

    Answer: B

    std::uint8_t is another name for unsigned char on these platforms, and streams print character types as characters: 66 is the code of B. std::cout << +grade or std::println("{}", grade) prints 66.

  7. Question 7 of 13 What does this print?

    Read the code, then choose one answer.

    std::println("{}", std::in_range<std::int16_t>(40'000));
    Show the answer: Question 7 of 13

    Answer: false

    std::in_range<T>(value) is true exactly when value fits T. choose_types.cpp checks the same pair while compiling, with static_assert(!std::in_range<std::int16_t>(40'000)).

  8. Question 8 of 13 On Apple silicon, is plain char signed or unsigned?

    Choose one answer.

    Show the answer: Question 8 of 13

    Answer: Signed

    Plain char is signed on Apple silicon and on Windows, and unsigned on Linux for arm64. Bytes are therefore read as unsigned char, std::uint8_t or std::byte.

  9. Question 9 of 13 A function adds up the bytes of a file. Which parameter type makes it give the same result on every platform?

    Choose one answer.

    Show the answer: Question 9 of 13

    Answer: const unsigned char*

    Plain char is signed on some platforms and unsigned on others, so a byte such as 0xC8 adds -56 on one and 200 on another. unsigned char (or std::uint8_t, or std::byte) always reads a byte as 0 to 255. signed char is portable too, but it reads bytes above 127 as negative numbers, which a byte sum does not want.

  10. Question 10 of 13 What does std::cout << true << '\n'; print?

    Choose one answer.

    Show the answer: Question 10 of 13

    Answer: 1

    bools.cpp printed "cout prints true as 1, with std::boolalpha as true": streams print numbers for bool until you set std::boolalpha.

  11. Question 11 of 13 What is the value of total?

    Read the code, then choose one answer.

    bool flag = -3;
    int total = flag + true;
    Show the answer: Question 11 of 13

    Answer: 2

    -3 is not zero, so flag is true. In arithmetic both bools are promoted to int, so the sum is 1 + 1.

  12. Question 12 of 13 How many bytes is a long double on Apple silicon?

    Choose one answer.

    Show the answer: Question 12 of 13

    Answer: 8

    On Apple silicon long double is the same 8-byte format as double, which makes it the least portable fundamental type: on the standard Arm ABI it is a 16-byte quad-precision type.

  13. Question 13 of 13 Ten million sensor readings each need about 4 significant decimal digits. Which type keeps them in the least memory?

    Choose one answer.

    Show the answer: Question 13 of 13

    Answer: float

    Prefer double, unless memory is tight, as in a large array: then a float's 6 digits may be enough, at half the size.

Interview questions

Fresher · Is an int always 32 bits in C++?

No. The standard only requires an int to be at least 16 bits wide, and no narrower than short. It is 32 bits on all mainstream 32-bit and 64-bit desktop, server and phone platforms, which is why people assume it, but small embedded processors may use 16. The type that really differs between today’s platforms is long: 8 bytes on 64-bit Linux and macOS, 4 bytes on 64-bit Windows. When a program needs an exact width, it should say so with std::int32_t or std::int64_t from <cstdint>, and it can check an assumption at compile time with static_assert(sizeof(int) == 4);.

Expect the interviewer to go one step further into what a platform may change. Answer both follow-ups below, then read why each option holds up or fails:

Checkpoint

Interview round: two follow-up questions

  1. Question 1 of 2 The interviewer follows up: a function relies on int being 32 bits. How do you make the build fail on a platform where it is not?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: static_assert(sizeof(int) == 4); next to the code that relies on it

    An assumption the code depends on belongs in a static_assert. Where the code really needs exactly 32 bits, std::int32_t says so in the type itself.

  2. Question 2 of 2 And then: a library must build with every conforming compiler, small embedded ones included. Which range can it rely on for int?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: -32,768 to 32,767

    Portable code relies on the standard's minimum: int holds at least -32,768 to 32,767. A library that needs more says so with long long or a fixed-width type such as std::int32_t.

Model answers open with any pass: ₹79 for 1 week ($2.99), one payment that never renews.

  1. Mid to senior

    A tool writes struct Header { long timestamp; char kind; unsigned size; }; to disk with one fwrite on 64-bit Linux, and the Windows build of the same tool reads garbage from those files. List every reason, and design a header layout that any compiler reads the same way.

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  2. Fresher to mid

    A timing helper returns the milliseconds since start-up as a long. It works on Linux servers, but on Windows machines that stay on for a few weeks, timeouts suddenly fire at once or never fire. When exactly does it break, and how do you fix the type?

    Model answer, Pro. First step and follow-ups

Key takeaways

  • The standard fixes minimum widths (short and int 16 bits, long 32, long long 64), not sizes; the platform decides the rest. 64-bit macOS and Linux are LP64 (long is 8 bytes) and 64-bit Windows is LLP64 (long is 4 bytes), so avoid plain long in portable code.
  • sizeof gives sizes in bytes, and std::numeric_limits<T> gives lowest(), max(), digits10 and more, at compile time; N bits hold -2ᴺ⁻¹ to 2ᴺ⁻¹ - 1 signed and 0 to 2ᴺ - 1 unsigned.
  • Use std::int32_t, std::int64_t and the other <cstdint> types where the width matters, and std::in_range to check that a value fits.
  • std::uint8_t is a character type underneath: std::cout prints it as a character, std::println as a number.
  • Plain char is signed on Apple silicon and Windows and unsigned on Linux for arm64. Use unsigned char or std::byte for bytes.
  • Non-zero converts to true and true to 1, so compare an int with 0, never with true; long double is just a double on Apple silicon.

Terms in this lesson

data model
The sizes a platform gives int, long, long long and pointers. 64-bit macOS and Linux use LP64, where long and pointers are 64 bits; 64-bit Windows uses LLP64, where long stays 32 bits.
fixed-width integer type
An integer type of <cstdint> whose name gives its exact width, such as std::int32_t or std::uint8_t: exactly that many bits, with no padding bits, on every platform that provides it.
two's complement
The way signed integers are stored, the only one C++20 allows: the top bit counts as minus 2 to the power N − 1, so N bits hold −2ᴺ⁻¹ to 2ᴺ⁻¹ − 1, and the bits 1111 1111 of a signed byte are −1.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.