Cambo Freelance
ទំព័រដើមសេវាកម្មអត្ថបទការបង្រៀនក្រុមវគ្គបណ្តុះបណ្តាលទំនាក់ទំនង
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

ផ្ញើសាររបស់អ្នក

ត្រៀមចាប់ផ្តើមគម្រោងរបស់អ្នក? ទំនាក់ទំនងក្រុមរបស់យើង។

ទំនាក់ទំនង

សេវាកម្ម

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

តំណភ្ជាប់មានប្រយោជន៍

  • ទំព័រដើម
  • សេវាកម្ម
  • ការសិក្សា
  • អំពីយើង
  • ទំនាក់ទំនង
  • តម្លៃ

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Constants
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 8 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++BeginnerLesson 8 of 50CamboFreelanceJune 19, 2026

C++ Constants

Learn how to declare constants in C++ using const, #define, and constexpr. Understand when and why to use each approach.


Tutorials › C++ › Constants
Beginner5 min readLesson 8 of 50

In This Lesson

  • const Keyword
  • #define Macro
  • constexpr
  • Quiz

The const Keyword

Use const to declare a variable whose value cannot be changed after initialization. Constants make code safer and more readable.

#include <iostream>
using namespace std;

int main() {
    const double PI = 3.14159265;
    const int MAX_SIZE = 100;
    const string GREETING = "Hello";

    cout << "PI = " << PI << endl;
    cout << "Max Size = " << MAX_SIZE << endl;
    cout << GREETING << endl;

    // PI = 3.0; // ERROR: cannot assign to const variable
    return 0;
}

Output:

PI = 3.14159265
Max Size = 100
Hello

#define Preprocessor Macro

Before C++11, #define was the common way to create constants. It is a preprocessor directive — the compiler replaces every use of the name with the value before compiling.

#include <iostream>
#define MAX_SCORE 100
#define GAME_NAME "Space Shooter"
using namespace std;

int main() {
    cout << "Game: " << GAME_NAME << endl;
    cout << "Max Score: " << MAX_SCORE << endl;
    return 0;
}
Prefer const over #define: const is type-safe, respects scope, and shows up in debuggers. Use #define only when necessary for legacy code.

constexpr (C++11)

constexpr means the value is computed at compile time, making programs faster:

constexpr int SQUARE(int x) { return x * x; }

int main() {
    constexpr int result = SQUARE(5);   // Computed at compile time
    cout << result;   // Output: 25
}

Exercise

Declare constants for: the number of days in a week (int), the value of gravity (double = 9.8), and the name of your app (string). Print all three.

Show Solution
#include <iostream>
using namespace std;

int main() {
    const int DAYS_PER_WEEK = 7;
    const double GRAVITY = 9.8;
    const string APP_NAME = "MyApp";

    cout << "Days/Week: " << DAYS_PER_WEEK << endl;
    cout << "Gravity: " << GRAVITY << endl;
    cout << "App: " << APP_NAME << endl;
    return 0;
}

Quiz

  1. What happens if you try to change a const variable?

    • A) The value changes silently
    • B) A compile-time error occurs
    • C) A runtime error occurs
    • D) Nothing happens
    Answer

    B) The compiler will reject the assignment and produce an error.

  2. Which is type-safe: const or #define?

    • A) #define
    • B) Both are equally safe
    • C) const
    • D) Neither
    Answer

    C) const — it has a type, respects scope, and is visible to the debugger.

Summary

  • const declares a variable that cannot be modified after initialization.
  • #define creates preprocessor text-replacement macros (legacy approach).
  • constexpr evaluates the value at compile time for performance.
  • Prefer const or constexpr over #define in modern C++.
← Previous: Data Types Next: C++ User Input →
c++constantsconstbeginner
PreviousLesson 7: C++ Data TypesNextLesson 9: C++ User Input
Back to All Tutorials