Variables and Constants

A variable or a constant stores a value. You can refer to them using their names.

  • You CAN change the value stored by a variable.
  • You CANNOT change the value stored by a constant.

Defining a variable

Specify the data types, the name of the variable, and optionally its initial value.

The name of the variable should ONLY consist of letters (AZ, az), numbers (09), and/or underscores (_), AND it must begin with a letter or an underscore.

// Defining a variable named `a`
// that can store integers (int)
int a = 1234;

// Defining another variable named `b`.
// You can also not give them an initial value.
int b;

You can also declare multiple variables in one line:

int a, b, c;
int d = 10, e = 20;

Defining a constant

Similar to variable declaration, but add const before the type:

// You must always initialize constants
const int LUCKY_NUMBER = 85;

Initialization syntaxes

There are 4 syntaxes to initialize a variable (or constant) in C++. For consistency, you should only consider using copy initialization and braced initialization.

Default initialization

This means you do not initialize the variable and they may store an indeterminate value (whatever value that was previously stored in that address).

int my_number;

Copy initialization

Basically, you are copying the value/object on the right-hand side to the variable on the left-hand side.

  • For built-in type (e.g. int), this is the same as direct initialization (see next section).
  • For more complicated types, this can be less efficient than direct initialization in certain cases (learncpp.com). E.g., when you do T t = x it "constructs an implicit conversion sequence" to convert x to type T (stackoverflow.com).
int my_number = 2;
Pair pair = Pair(2, 3);

Direct initialization

Directly initializes the object. (Under the hood, this calls the corresponding constructor.)

int my_number(2);
Pair pair(2, 3);

Braced initialization

The uniform and preferred way to initialize a variable. (Under the hood, this performs direct initialization or copy initialization.)

It also prevents "narrowing" conversions (for example, trying to assign long long to int or float to int will cause compile error).

// Initialize to 2
// (both are equivalent)
int my_number{2};
int my_number = {2}

// Initialize to 0
// (all of them is equivalent)
int my_number{};
int my_number{0};
int my_number = {0}

Local and global variables

Local variables

Variables defined inside a function or inside a compound statement block ({...}) are local variables. You can access local variables in their scope only (inside their function or their nearest enclosing block).

int main() {
  // `my_number` is a local variable.
  // You can only access `my_number` inside `main()`
  int my_number = 5;

  // Using the local variable `my_number`
  printf("%d\n", my_number); //=> 5
}

Global variables

Variables defined outside a function are global variables. You can access global variables anywhere inside the current file.

// `MY_NUMBER` is a constant
// You can access `MY_NUMBER` anywhere
int MY_NUMBER = 10;

int main() {
  // Using the global variable `MY_NUMBER`
  printf("%d\n", my_number); //=> 10
}

Operations with variables and constants

Using a variable's or constant's value

Just specify their name to get their stored value.

int a = 1;
int b = 2;

int main() {
  // `c` will be 1 + 2 = 3
  int c = a + b;
  printf("%d\n", c); //=> 3
}

Changing a variable's values

Use the assignment operator. (Remember that you cannot re-assign a constant.)

int main() {
  int my_number = 1;
  printf("%d\n", my_number); //=> 1

  // Change the value stored by
  // `my_number` into 2
  my_number = 2;
  printf("%d\n", my_number); //=> 2
}

Variable shadowing

You cannot declare the same variable name more than once in the same nesting level.

However, if the same name occurs in different nesting level, then you will use the variable closer to the current scope. (This behavior is known as variable shadowing, because the nested variable declaration "hides" the outer variable declaration.)

int my_number = 10;

int main() {
  int my_number = 20;
  // Uses the variable closer to the current scope
  printf("%d\n", my_number); //=> 20
}

References