Search CTRL + K

Constant

常量(constant) 是指不能改变的值。在 cpp 中,有两种类型常量:

命名常量

在 cpp 中,命名常量有三种子类型:

Introduction to constants

In programming, a constant is a value that may not be changed during the program’s execution.[1]

C++ supports two different kinds of constants:

  • Named constants are constant values that are associated with an identifier. These are also sometimes called symbolic constants, or occasionally just constants.
  • Literal constants are constant values that are not associated with an identifier.
Types of named constants

There are three ways to define a named constant in C++:[1:1]

  • Constant variables
  • Object-like macros with substitution text
  • Enumerated constants
What is literals?

Literals are values that are inserted directly into the code. Literals are sometimes called literal constants because their meaning cannot be redefined.[2]


  1. https://www.learncpp.com/cpp-tutorial/constant-variables-named-constants/ ↩︎ ↩︎

  2. https://www.learncpp.com/cpp-tutorial/literals/ ↩︎