
Int read_int() Ĭonst int RUN_TIME= read_int() // set at runtime can be any value For example:Ĭonst int COMPILE_TIME = 5 // set at compile-time its value is 5 (five) Note that once initialized, neither type of constants can change its value-they're constant.
A runtime constant can have a different value each time the application is run. A compile-time constant will have the same value each time the application is run. If you run the same program more than once: A runtime constant is a value that is computed only while the program is running. Grab your coffee and get focused, because it's going to get tough.Ī compile-time constant is a value that can be (and is) computed at compile-time. But even so, the process is far from simple. You'll see that changing a constant from compile-time to runtime or back will require you to change only one line of code-its definition. The reverse process is as easy-and this is important, since after profiling, you might decide to keep some compile-time constants to make your application run faster. In this article, I'll show you several ways to change from compile-time constants to runtime constants. You'll have to run it multiple times with different values for constants, because changing one value will require recompiling your application. Even worse, try profiling your application.
You'll have to tune some constants' values, and you could end up with a plethora of configurations ( Listing A). Problems can also strike while you're testing and debugging your application, or when you need to stress test some part of it.
The trouble starts when you realize that some constants are lost at compile time-they must be read at runtime (after the program starts), from a file, registry, database, etc. "Use constants in code!" Everybody tells you this, and they're right.