Variable (C++)

A variable in C++ is a named storage location with a fixed type that holds a value. Unlike some languages, you must declare a variable's type before using it.

C++

C++ is statically typed, so the type you give a variable (like int or double) is checked at compile time and cannot change later. You can let the compiler deduce the type with the auto keyword.

A variable should be initialized when declared; using an uninitialized variable leads to unpredictable values.

Syntax

type name = value;

Examples

C++

#include <iostream>

int main() {
    int score = 10;
    double price = 4.99;
    auto total = score + 1;
    std::cout << total << " " << price << std::endl;
    return 0;
}

Output:

11 4.99

Learn it in a lesson

See also

Back to the Dictionary