C++ Variables

A variable is a named place in memory with a type. The type says which values it can keep and how much memory it uses.

Declare and initialize

#include <iostream>
#include <string>

int main() {
    int age = 21;                 // copy initialization
    double price{99.5};           // brace initialization (preferred)
    std::string name = "Riya";
    bool active = true;
    char grade = 'A';

    std::cout << name << " " << age << " " << price << " " << active << " " << grade << '\n';
}
Output

Riya 21 99.5 1 A

Always initialize

A local variable without a value keeps garbage from the memory. Reading it is undefined behavior. The compiler does not stop you, but with -Wall it warns you (this is the real warning text):

#include <iostream>

int main() {
    int x;
    std::cout << x << '\n';
}
Output

Garbage.cpp: In function 'int main()':
Garbage.cpp:5:18: warning: 'x' is used uninitialized [-Wuninitialized]
5 | std::cout << x << '\n';
| ^
Garbage.cpp:4:9: note: 'x' was declared here
4 | int x;
| ^

Brace initialization stops narrowing

int main() {
    int a = 3.7;       // allowed, the value becomes 3 (maybe a warning)
    int b{3.7};        // error: narrowing conversion
}
Output

Narrow.cpp: In function 'int main()':
Narrow.cpp:3:11: error: narrowing conversion of '3.7000000000000002e+0' from 'double' to 'int' [-Wnarrowing]
3 | int b{3.7}; // error: narrowing conversion
| ^~~
Narrow.cpp:2:9: warning: unused variable 'a' [-Wunused-variable]
2 | int a = 3.7; // allowed, the value becomes 3 (maybe a warning)
| ^
Narrow.cpp:3:9: warning: unused variable 'b' [-Wunused-variable]
3 | int b{3.7}; // error: narrowing conversion
| ^

auto: let the compiler find the type

#include <iostream>
#include <typeinfo>

int main() {
    auto count = 10;          // int
    auto rate = 4.5;          // double
    auto text = "hi";         // const char*
    std::cout << typeid(count).name() << " " << typeid(rate).name() << " " << typeid(text).name() << '\n';
}
Output

i d PKc

The letters are the compiler code of the types (i = int, d = double, PKc = pointer to const char). auto is still strongly typed.

const and constexpr

int main() {
    const double PI = 3.14159;
    PI = 3;
}
Output

Const.cpp: In function 'int main()':
Const.cpp:3:8: error: assignment of read-only variable 'PI'
3 | PI = 3;
| ~~~^~~

const means the value can not change after the start. constexpr means the value is known when the program is compiled, so it can be used for array sizes and in other compile time work.

#include <array>
#include <iostream>

constexpr int size = 4;

int main() {
    std::array<int, size> a{1, 2, 3, 4};
    std::cout << a.size() << '\n';
}
Output

4

Scope

A variable lives only inside the block { } where you declare it. A variable declared inside the for line lives only inside the loop.

Naming rules

  • Letters, digits and _. Not starting with a digit.
  • Do not start with an underscore and a capital letter, or use two underscores: these names are reserved.
  • Use meaningful names: total_price, not tp.
Note: Prefer const for everything which does not change. It makes bugs easier to find. Next: C++ Data Types.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.