← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 15 of 15

C++ Constants

A constant is a value you promise never to change, like the number of days in a week. C++ lets you lock it with const or constexpr.
Syntax
cpp
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

cpp
// 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;
}

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)

cpp
#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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to change a const variable later, such as PI = 3;, which is a compile error.
  2. Declaring const int x; without an initial value, which is an error because a constant must be initialized.
  3. Ending a #define line with a semicolon like #define MAX 100;, which then gets pasted into every use and breaks expressions.
Chapter Summary
  • 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, and char describe 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.