Constants, Literals, and Strings recap

You've now learned about constants, literals, and strings in C++. Let's review the key concepts.

Constants and Named Values

A constant is a value that may not be changed during program execution. C++ supports two types of constants: named constants and literals.

A named constant is a constant value associated with an identifier. A literal constant is a constant value not associated with an identifier.

Constant Variables

A variable whose value cannot be changed is called a constant variable. The const keyword can be used to make a variable constant. Constant variables must be initialized. Avoid using const when passing by value or returning by value.

A type qualifier is a keyword applied to a type that modifies how that type behaves. As of C++23, C++ only supports const and volatile as type qualifiers.

Compile-Time vs Runtime Constants

A constant expression is an expression that can be evaluated at compile-time. An expression that is not a constant expression is sometimes called a runtime expression.

A compile-time constant is a constant whose value is known at compile-time. A runtime constant is a constant whose initialization value isn't known until runtime.

A constexpr variable must be a compile-time constant and initialized with a constant expression. Function parameters cannot be constexpr.

Literals and Magic Numbers

Literals are values inserted directly into the code. Literals have types, and literal suffixes can be used to change the type of a literal from the default type.

A magic number is a literal (usually a number) that either has an unclear meaning or may need to be changed later. Don't use magic numbers in your code. Instead, use symbolic constants.

Numeral Systems

In everyday life, we count using decimal numbers, which have 10 digits. Computers use binary, which only has 2 digits. C++ also supports octal (base 8) and hexadecimal (base 16). These are all examples of numeral systems, which are collections of symbols (digits) used to represent numbers.

Strings in C++

A string is a collection of sequential characters used to represent text (such as names, words, and sentences). String literals are always placed between double quotes. String literals in C++ are C-style strings, which have a strange type that is hard to work with.

std::string offers an easy and safe way to deal with text strings. std::string lives in the <string> header. std::string is expensive to initialize (or assign to) and copy.

std::string_view provides read-only access to an existing string (a C-style string literal, a std::string, or a char array) without making a copy. A std::string_view that is viewing a string that has been destroyed is sometimes called a dangling view. When a std::string is modified, all views into that std::string are invalidated, meaning those views are now invalid. Using an invalidated view (other than to revalidate it) will produce undefined behavior.

Because C-style string literals exist for the entire program, it is okay to set a std::string_view to a C-style string literal, and even return such a std::string_view from a function.

A substring is a contiguous sequence of characters within an existing string.

Key Terminology

  • Constant: A value that may not be changed during program execution
  • Named constant: A constant value associated with an identifier
  • Literal constant: A constant value not associated with an identifier
  • Constant variable: A variable whose value cannot be changed
  • const keyword: Keyword used to make a variable constant
  • Type qualifier: A keyword applied to a type that modifies how that type behaves
  • Constant expression: An expression that can be evaluated at compile-time
  • Runtime expression: An expression that is not a constant expression
  • Compile-time constant: A constant whose value is known at compile-time
  • Runtime constant: A constant whose initialization value isn't known until runtime
  • constexpr: Keyword for variables that must be compile-time constants
  • Literal: A value inserted directly into the code
  • Magic number: A literal with an unclear meaning or that may need to be changed later
  • Decimal: Base-10 numeral system (10 digits)
  • Binary: Base-2 numeral system (2 digits)
  • Octal: Base-8 numeral system (8 digits)
  • Hexadecimal: Base-16 numeral system (16 digits)
  • Numeral system: A collection of symbols (digits) used to represent numbers
  • String: A collection of sequential characters used to represent text
  • C-style string: String literals in C++ with a strange type that is hard to work with
  • std::string: A safe and easy way to deal with text strings in C++
  • std::string_view: Provides read-only access to an existing string without making a copy
  • Dangling view: A std::string_view viewing a string that has been destroyed
  • Invalidated view: A view into a std::string that has been modified
  • Substring: A contiguous sequence of characters within an existing string

Looking Forward

Understanding constants, literals, and strings is fundamental to writing clear and maintainable C++ code. These concepts will be used throughout your programming journey, from simple calculations to complex text processing. The distinction between compile-time and runtime constants becomes increasingly important as you write more sophisticated programs. Practice using these concepts to solidify your understanding.