Variables, expressions, and operators
Variables in C++ are used for storing values which are usually input, manipulated, and output. Variables must be explicitly declared as a specific data type (e.g. basic data types) before usage.
Contents
Identifiers
As in regular mathematics, variables have a name that we call an identifier in C++. Identifiers have various rules and conventions.
Identifier rules
Identifiers must adhere to the following rules:
- May only contain alphabetic characters, digits, and underscores
- May not contain any special characters or spaces
- May not start with a digit
- May not be a reserved word
- May not be identical to other identifiers in the same scope
Identifier conventions
Although the following are all legal, identifiers should typically:
- Not start with an underscore (these are frequently used for header files)
- Not be all capital letters, unless used as a constant
- Be descriptive about what is to be stored in them to help with code readability
Identifiers may, additionally, adhere to one of many established naming conventions.
Declaration
A variable can be declared as datatype identifier;
Multiple variables can be declared in one line, for example int x, y, z;
Expressions
An expression is any combination of values, variables, and operators that can be evaluated. The following are all examples of expressions:
5 + 6
5 + 8 * 8
8 / 4.1 + 6 * 3
x // assuming x is a declared variable
x + 5
"Hello"
'A'
'A' + 5
Assignment
Variables can be assigned the value of an expression if that expression can be evaluated to the same data type as the variable. Some examples are provided below.
int x = 5, y;
y = x * 2;
x = y;
x = 50;
x = 50 + (y + x) * 2;
Note that assignment is associative, so a = b = 5;
will assign the value of 5 to both variables. Also note that assignment is an expression and evaluates to the variable itself, but it must be placed in parentheses. For example cout << (x = 5 + 6);
will both output 11 and assign it to the variable x
.
Initialization
Variables can be initialized upon declaration or their values can be set later (directly or via input). Variables should always have some value placed in them before they are used in any calculations or output, otherwise erroneous results may occur.
int age = 30; // Initialization upon declaration
double rate;
rate = 0.05; // Not initialization, but a valid method for placing a value in the variable before it is used in a calculation
double fee;
cin >> fee; // Not initialization, but a valid method for placing a value in the variable before it is used in a calculation
Mathematical operations
Standard mathematical operations for integral and floating-point values are + - * / %
.
The modulo operator, %
, is the remainder when an integral value is divided by another, for example 10 mod 3 = 1, 100 mod 8 = 4, and 20 mod 5 = 0.
The order of operations is similar to regular mathematics, where multiplication and division are evaluated before addition and subtraction. The modulo operator is at the same precedence level as multiplication and division.
To enforce an alternate order of operations, parentheses can be used as in regular mathematics, for example 2 + 3 * 5 = 17 but (2 + 3) * 5 = 25.
Mixed expressions
A mixed expression is an expression that contains both integral and floating-point data types. The general rule for evaluation is that operators of the same data type evaluate to a result of the same data type, while mixed expressions implicitly convert (or cast) integral values to floating-point values first. Some examples are provided below:
3.0 / 4.0 = 0.75 // both operands are floating-point, the result is floating point
3 / 4 = 0 // both operands are integral, the result is integral -- it is truncated, not rounded
3.0 / 4 = 0.75 // the integral operand is converted to 4.0 and the result is floating-point
3 / 4.0 = 0.75 // the integral operand is converted to 4.0 and the result is floating-point
Typecasting
Explicit conversion from one data type to another can be done using typecasting. There are several ways of typecasting:
- Functional cast:
datatype(expression)
- C-style cast:
(datatype)expression
- Static cast:
static_cast<datatype>(expression)
For example:
double d = 5.84;
int i;
// all 3 of the below assign the value of 5 to i (it is truncated, not rounded)
i = static_cast<int>(d);
i = int(d);
i = (int)d;
The standard way of doing typecasting in C++ is with static_cast
.
Constants
Constants can be created by putting the const
keyword in front of the datatype. A constant can be initialized but never changed after that. It is standard to use all capital letters for constant identifiers.
const int DRINKING_AGE = 21;
const double GOLDEN_RATIO = 1.618;
const double PI = 3.14;
Combined assignment operators
Although operations on variables such as x = x + 2;
are common, there exist combined assignment operators for each of the five mathematical operations
x += expr; // equivalent to x = x + (expr);
x -= expr; // equivalent to x = x - (expr);
x *= expr; // equivalent to x = x * (expr);
x /= expr; // equivalent to x = x / (expr);
x %= expr; // equivalent to x = x % (expr);