Runtime and Compile-time constants in C++ Read Courses Practice Improve Improve Improve Like Article Like Save Article Save Report issue Report Run-time Constant: These are the constants whose respective values can only be known or computed at the time of running of source code. Run time Constants are a bit slower than compile-time constants but are more flexible than compile-time constants. However, once it is initialized, the value of these constants can’t be changed. Below is the program for illustration of Runtime constants: C++ // C++ program to illustrate // Run-time Constants #include <iostream> using namespace std; // Driver Code int main() { // Input a variable double electonmass; cin >> electonmass; // Define a constant // and initialize it at // run-time const double electon_mass{ electonmass }; // Known to the compiler // at the run-time cout << electon_mass << endl; return 0; } Output: 2.07335e-317 Compile-time Constant: These are the constants whose respective value is known or computed at the time of compilation of source code. Compile-time constants are faster than run-time constants but are less flexible than run-time constants. Below is the program for illustration of Compile-time Constant: C++ // C++ program to illustrate // compile-time constants #include <iostream> using namespace std; // Driver Code int main() { // Declare and initialize // compile time constant const double electron_q{ 1.6e-19 }; // Value known to compiler // at compile-time cout << electron_q << endl; return 0; } Output: 1.6e-19 Difference between Run-time and Compile-time constants Compile-time constants Run-time constants 1. A compile-time constant is a value that is computed at the compilation-time. Whereas, A runtime constant is a value that is computed only at the time when the program is running. 2. A compile-time constant will have the same value each time when the source code is run. A runtime constant can have a different value each time the source code is run. 3 It is generally used while declaring an array size. It is not preferred for declaring an array size. 4 If you use const int size = 5 for defining a case expression it would run smoothly and won’t produce any compile-time error. Here, if you use run-time constant in your source code for defining case expression then it will yield a compile-time error. 5 It does not produces any compile time error when used for initializing an enumerator. Same compilation error, if runtime constant is used for initializing an enumerator. Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now! Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about! Last Updated : 19 Oct, 2020 Like Article Save Article Previous How variable length argument works? Next Difference between sizeof(int *) and sizeof(int) in C/C++ Share your thoughts in the comments Add Your Comment Please Login to comment...