What is the difference between constexpr and const?

Last updated 21 June 2026

  • const means immutable after initialization, but the value may be computed at runtime (e.g. const int n = readInput();).

  • constexpr requires a compile-time constant, usable for array sizes, template arguments and case labels. Every constexpr variable is implicitly const.

  • constexpr functions run at compile time with constant arguments and at runtime otherwise.

  • https://www.geeksforgeeks.org/cpp/understanding-constexper-specifier-in-cpp/
  • https://jaganinfo.in/c-interview-questions-and-answers-for-experienced-developers
  • https://www.freecodecamp.org/news/cpp-constexpr-and-constinit/
All C++ interview questions