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:
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:
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.
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.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Data type of character constants in C and C++
- Virtual Functions and Runtime Polymorphism in C++ | Set 1 (Introduction)
- Output of C programs | Set 60 (Constants)
- Runtime Polymorphism in various types of Inheritance in C++
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- fesetround() and fegetround() in C++ and their application
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Even digits Sum and Odd digits sum divisible by 4 and 3 respectively
- Get first and last elements from Array and Vector in CPP
- Similarities and Differences between Ruby and C++
- Generate an array of given size with equal count and sum of odd and even numbers
- Similarities and Difference between Java and C++
- INT_MAX and INT_MIN in C/C++ and Applications
- Change/add only one character and print '*' exactly 20 times
- Comma in C and C++
- How does "void *" differ in C and C++?
- What are the operators that can be and cannot be overloaded in C++?
- Can we use function on left side of an expression in C and C++?
- delete and free() in C++
- ASCII NUL, ASCII 0 ('0') and Numeric literal 0
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.