C++

C++ Variables and Data Types: Declaration, Initialization, and Sizes

The same program shows why braces reject a narrowing conversion that equals accepts, why an uninitialized variable earns a compiler warning, and why bool prints as 1 until you ask for true.

Written November 11, 2026 · Tested with GCC 13.3.0 · 3 min read

By the end of this guide
  • Declaration versus initialization
  • Brace initialization rejects narrowing
  • The fundamental types and their sizes
  • auto deduces the type from the initializer

What this guide is

A variable is a named object with a type, a value, and a size in bytes. The program uses GCC 13.3.0 with -std=c++17 -Wall -Wextra and exits with code 0.

The program

C++
#include <iostream>
#include <string>
int main() {
    int count = 42;
    double price = 9.99;
    bool ready = true;
    char grade = 'A';
    std::string name = "veldica";
    auto inferred = 100;
    const int max_users = 10;
    std::cout << count << " " << price << " " << ready << " " << grade << " " << name << "\n";
    std::cout << "int=" << sizeof(int) << " double=" << sizeof(double) << " bool=" << sizeof(bool) << "\n";
    std::cout << "inferred=" << inferred << " max=" << max_users << "\n";
    return 0;
}

The exact output:

Output
42 9.99 1 A veldica
int=4 double=8 bool=1
inferred=100 max=10

Three details in the output deserve a second look before the sections below explain them. First, ready prints as 1, not true: bool prints as a number until std::boolalpha is set. Second, inferred is an int, not a mystery type: auto deduces the type from the initializer, and 100 is an int. Third, max_users is a const int, which means it cannot be assigned to after this line, and the compiler enforces that.

Declaration versus initialization

A declaration introduces a name and its type. An initialization gives the object its first value. int count; declares without initializing, and count then holds an indeterminate value until something assigns to it. int count = 42; declares and initializes in one statement, and count is 42 from the first use. The distinction matters because the compiler treats the two cases differently: it can warn about a declared-but-never-initialized variable that is read, and it cannot warn about a variable that was initialized to a wrong value.

C++ offers three spellings for the same initialization, and they are not interchangeable. Copy initialization uses =, as in int count = 42;. Direct initialization uses parentheses, as in int count(42);. List initialization uses braces, as in int count{42};. For a plain int holding 42, all three produce the same value. They differ when the initializer does not fit the type, and that difference is the subject of the next section.

Brace initialization rejects narrowing

A narrowing conversion is an implicit conversion that loses information: a floating-point value to an integer, a larger integer to a smaller integer that cannot hold it, or a pointer to bool. The C++ standard states the rule plainly for list initialization: if a narrowing conversion is required to convert any of the arguments, the program is ill-formed. The compiler must reject it, not warn about it.

The box run proves the rule with the smallest possible case:

C++
int x{3.14}; // error: narrowing conversion of '3.1400000000000001e+0' from 'double' to 'int' [-Wnarrowing]

That program does not compile. The same value with copy initialization does compile:

C++
int y = 3.14; // compiles; y is 3

The = form silently truncates 3.14 to 3. No warning was printed, so nothing in the build output flags the loss. The brace form turns the same mistake into a compile error at the line where it happens, which is the entire reason to prefer braces for fundamental types: the check runs at compile time, on the exact line, before the program ever runs. Empty braces value-initialize instead of leaving the object indeterminate, so int x{}; is a zero, not a gamble.

The fundamental types and their sizes

The sizes below are measurements from the test machine, not guarantees from the standard. The standard guarantees only minimums: int is at least 16 bits, and on 32-bit and 64-bit systems it is almost always at least 32 bits. The measured sizes, from a program compiled with the same GCC 13.3.0, are:

Type Size Holds
char 1 byte A single character, such as 'A'
bool 1 byte true or false, printed as 1 or 0 by default
short 2 bytes Small integers
int 4 bytes Whole numbers, such as 42
long 8 bytes Larger whole numbers
long long 8 bytes The largest whole numbers in the fundamental set
float 4 bytes Single-precision decimal numbers
double 8 bytes Double-precision decimal numbers, such as 9.99
std::string Not a fundamental type Text, such as "veldica"; its size is the size of the object, not the text

Two cautions belong with the table. First, long is 8 bytes here because this is a 64-bit Linux system; on 64-bit Windows it is 4 bytes, so a program that assumes long is 8 bytes is assuming a platform, not the language. Second, sizeof(std::string) would report the size of the string object itself, not the length of "veldica", so the table leaves it out rather than printing a number that invites the wrong reading. The length of the text is name.size(), which is 7.

auto deduces the type from the initializer

auto is a placeholder type specifier: the compiler deduces the type from the initializer, using the same rules as template argument deduction. In the program, auto inferred = 100; makes inferred an int, because 100 is an int literal, and the output confirms it: inferred=100. Nothing about auto is dynamic. The type is fixed at compile time, and inferred cannot later hold a double any more than a declared int can.

auto earns its place when the type is long, obvious from the initializer, or unutterable, as with most iterator and lambda types. It does not earn its place where the type is the documentation. auto price = 9.99; hides that price is a double, and a reader who needs to know whether price is a float or a double must work it out from the literal. The rule that fits this guide is narrow: use auto when the initializer states the type plainly, and write the type out when the type is the fact the reader needs.

const means the value does not change

const int max_users = 10; declares an object whose value is fixed after initialization. An assignment to max_users later in the program is a compile error, not a runtime check. That is the whole mechanism: the compiler refuses the write, so the invariant holds without a test.

A constant is not a variable in the usual sense, and the distinction is worth stating plainly. A variable is storage you expect to change; a const object is storage you promise not to change. Marking a value const at the point of declaration is the cheapest way to record that promise, because the compiler enforces it at every later line for free.

std::string versus C-style strings

std::string name = "veldica"; holds text in an object that manages its own storage, knows its own length, and compares with ==. A C-style string, const char* name = "veldica";, is a pointer to the first character of an array, and it knows nothing about its own length. The two look alike in a print statement and behave differently everywhere else: comparing two std::string values with == compares the text, while comparing two const char* values with == compares the pointers. New code should reach for std::string first and use C-style strings only where an interface demands them.

Trap 1: reading an uninitialized variable

Trap
Declaring without initializing leaves the value indeterminate, and reading it is undefined behavior. The compiler can catch the easy case. This program, compiled with -O2 so the optimizer can see the use:
C++
#include <iostream>
int main(){int x; std::cout<<x;}

produces this warning:

Output
/tmp/uninit.cpp: In function 'int main()':
/tmp/uninit.cpp:2:30: warning: 'x' is used uninitialized [-Wuninitialized]

Note the warning flag. It is -Wuninitialized, not -Wmaybe-uninitialized: the compiler is certain here, because the read follows the declaration with nothing in between. The warning is a gift that disappears in harder cases: once the read is behind a branch or a function call, the compiler may stay silent and the program still reads garbage. The fix is the same in both cases. Initialize at the point of declaration, and prefer int x{}; when there is no meaningful value yet, so the variable is zero rather than indeterminate.

Trap 2: integer division truncates

Trap
5 / 2 is 2 in C++, not 2.5. When both operands are integers, the division is integer division, and the fractional part is discarded. The box run confirms it: 5 / 2 prints 2, while 5.0 / 2 prints 2.5, because one double operand makes the whole division floating-point. The trap bites when the operands are variables rather than literals: total / count with two int variables truncates silently, and the program compiles cleanly either way. The fix is to make one operand a floating-point value, either by writing 5.0 or by casting, as in static_cast<double>(total) / count.

Trap 3: bool prints as 1 and 0

Trap
The first line of the program output shows ready as 1, not true. That is the default: std::cout prints a bool as a number. The fix is a stream manipulator, verified:
C++
std::cout << std::boolalpha << ready << "\n"; // prints true

std::boolalpha stays set on the stream until it is turned off with std::noboolalpha, so a program that sets it once prints every later bool as true or false. The value never changes, only its printed form, and a reader who sees 1 in output should check whether the value is a bool before assuming it is a count.

Checklist

  • Before declaring a variable, decide whether it needs a first value now: if it does, initialize it in the same statement, and prefer braces so a narrowing conversion becomes a compile error.
  • Before trusting a sizeof number, check which machine produced it: long is 8 bytes on this 64-bit Linux box and 4 bytes on 64-bit Windows, so the table in this guide is a measurement, not a guarantee.
  • Before using auto, check whether the initializer states the type plainly: auto inferred = 100; is an int, and a reader who needs the type from the declaration alone will not find it there.
  • Before marking a value const, check that it truly never changes: the compiler enforces the promise at every later line, which is the point.
  • Before comparing text, check which string type you hold: == on two std::string values compares the text, while == on two const char* values compares the pointers.
  • Before reading a variable, check that it was initialized: a bare int x; read earns a -Wuninitialized warning in the easy case and silence in the hard ones.
  • Before dividing two integers, check whether you wanted a fraction: 5 / 2 is 2, and only a floating-point operand makes it 2.5.
  • Before reading 1 or 0 in output, check whether the value is a bool: the default print is numeric, and std::boolalpha is what prints true and false.

Sources

Keep Exploring