C++ Constants
In this page:
const data_type CONSTANT_NAME = value;
constexpr data_type CONSTANT_NAME = compile_time_value;
The const Keyword
The const keyword tells the compiler that a variable's value must never change after it's initialized, and any attempt to assign to it later is caught as a compile-time error rather than a runtime bug.
This is useful for values like a tax rate or an array size that should be fixed and protected from accidental modification elsewhere in the code.
Example: The const Keyword
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare taxRate and set it to 0.08
const double taxRate = 0.08;
// Print to the console with cout
std::cout << "Tax rate: " << taxRate << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constant Expressions (constexpr)
constexpr goes a step further than const by requiring the value to be computable entirely at compile time, which lets the compiler substitute the literal result directly into the generated machine code instead of computing it while the program runs.
This can measurably improve performance for values used in tight loops or as array sizes.
Example: Constant Expressions (constexpr)
#include <iostream>
constexpr int arraySize = 10; // computed entirely at compile time
int main() {
int values[arraySize];
std::cout << "Array size: " << arraySize << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Literals
A literal is a value written directly into your source code exactly as it will be used — 42, 3.14, A, and "hello" are all literals of type int, double, char, and string respectively.
Literals are distinct from named constants: a literal has no name of its own, while a constant gives a literal a readable, reusable label.
Example: Literals
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << 42 << " " << 3.14 << " " << 'A' << " " << "hello" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Defining Constants with #define
#define is a preprocessor directive that performs a pure text substitution before compilation even begins, replacing every occurrence of the defined name with its value throughout the file.
Because it happens at the text level rather than through the type system, modern C++ generally prefers const or constexpr, which are type-checked and easier to debug.
Example: Defining Constants with #define
// Include std::cout and std::cin
#include <iostream>
// Define the macro MAX_USERS (replaced by the preprocessor)
#define MAX_USERS 100
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << "Max users: " << MAX_USERS << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constant Best Practices
Writing constant names in ALL_CAPS, like MAX_USERS or PI, makes them instantly recognizable as fixed values whenever they appear in code, distinguishing them at a glance from ordinary variables.
Grouping related constants together near the top of a file also makes it easy to find and update configuration-like values in one place.
Example: Constant Best Practices
// Include std::cout and std::cin
#include <iostream>
// Declare PI and set it to 3.14159
const double PI = 3.14159;
// Declare MAX_USERS and set it to 100
const int MAX_USERS = 100;
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << PI << " " << MAX_USERS << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Trying to change a
constvariable later, such asPI = 3;, which is a compile error. - Declaring
const int x;without an initial value, which is an error because a constant must be initialized. - Ending a
#defineline with a semicolon like#define MAX 100;, which then gets pasted into every use and breaks expressions.
- C++ is a compiled language, and a first program shows its basic syntax, statements, comments, keywords, and identifiers.
- Data types such as numeric types,
bool, andchardescribe the values variables can hold. - Variables can be declared one at a time or several at once, and constants hold values that should not change.
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: