• Home
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React.js
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
    • TypeScript
      • Introduction to TypeScript
      • TypeScript Developer
    • React
      • Introduction to React
      • React Developer
    • Python
      • Introduction to Python
      • Python Developer
    • C++
      • Introduction to C++
      • C++ Developer
    • C Language
      • Introduction to C
      • C Developer
    • Rust
      • Introduction to Rust
      • Rust Developer
    • Zig
      • Introduction to Zig
      • Zig Developer
    • Linux Shell
      • Introduction to the Linux Shell
      • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Reading Grounds

Quick Links

  • About Us
  • Pricing
  • Partnership
  • Brainstorm
  • Terms
  • Privacy
  • Refunds

Courses

  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer

Newsletter

Subscribe to our free monthly newsletter, for a quick update on Python, JavaScript, and React news

© 2025 - 2026 STEMTrainingGrounds. All Rights Reserved.

C++ Developer -

Lesson 4 of 22

Lesson Progress: 0%

Lesson Progress: 0%
Lesson Incomplete
← Previous: Basic Data Types
Lesson 4 of 22
Next: Operators - Arithmetic →

A constant in C++ is a variable whose value cannot be changed after it is set. You create a constant by adding the keyword const before the type when you declare the variable. Constants are useful for values that should stay the same throughout your program, like the number of days in a week or the value of pi. Using constants makes your code easier to read and prevents accidental changes to important values. This lesson will show you how to create and use constants in C++.

Code Example
#include <iostream>
using namespace std;

int main() {
    const int DAYS = 7;
    cout << DAYS;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • const int DAYS = 7; creates a constant integer with the value 7.
  • The keyword const tells C++ that this value must never change.
  • The program prints 7.
Code Example
#include <iostream>
using namespace std;

int main() {
    const double PI = 3.14;
    cout << PI;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • const double PI = 3.14; creates a constant decimal value.
  • Constants work with any type, including double.
  • The program prints 3.14.
Code Example
#include <iostream>
using namespace std;

int main() {
    const string GREETING = "Hello";
    cout << GREETING;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • const string GREETING = "Hello"; creates a constant string.
  • Text constants are written in ALL CAPS by convention in C++.
  • The program prints Hello.
Code Example
#include <iostream>
using namespace std;

int main() {
    const int HOURS = 24;
    cout << HOURS * 7;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Constants can be used in calculations just like regular variables.
  • const int HOURS = 24; stores the number of hours in a day.
  • cout << HOURS * 7; multiplies to find hours in a week.
  • The program prints 168.
Code Example
#include <iostream>
using namespace std;

int main() {
    const int SCORE = 100;
    cout << SCORE;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Constants are perfect for fixed values like a maximum score.
  • const int SCORE = 100; sets a value that stays the same.
  • The program prints 100.
Code Example
#include <iostream>
using namespace std;

int main() {
    const int ROWS = 3;
    const int COLS = 4;
    cout << ROWS * COLS;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • You can use multiple constants together in an expression.
  • const int ROWS = 3; and const int COLS = 4; are two constants.
  • ROWS * COLS calculates their product to give 12.
  • Using constants makes the meaning of the numbers clear.

Test Incomplete

What keyword is used to create a constant in C++?

Question #

1/15

Score

0/0 - 0.0 %