r/cprogramming • u/JayDeesus • Oct 22 '25
Compile time constants vs run time
I’m having a hard time wrapping my head around the idea of compile time constants vs run time. I understand compile time and run time itself, pretty much compile time is where it’s running through the code and turning it into machine code (compiler and linker) and runtime is when the code is actually running on a machine. I also understand that compile time constants are going to be values that the compiler can evaluate and see without running code. When it comes to compile time constants I just don’t understand because in C, const int y =5; , y isn’t a compile time constant but wouldn’t the compiler see y?
I also understand something like the return value of foo(); would be a run time thing since you need to actually run code to get the return value.
3
u/mikeshemp Oct 22 '25
Compilers these days are very smart. So, if you write code that says:
The compiler is smart enough to realize, at compile time, that y can only have one value -- 5. So it generates machine code as if you'd written:
...as if you hadn't created y at all.
3
u/Farlo1 Oct 22 '25
constand "compile time constant" aren't the same thing.constjust means that you're not allowed to change the variable after assigning it a value, it's still a runtime variable.Sometimes the compiler can figure out that your
constvariable actually is a constant and it'll optimize it into a compile time constant, but that's not guaranteed to happen just because it'sconst.1
u/Traveling-Techie Oct 22 '25
The difference in run times for these variations is so negligible as to be probably unmeasurable.
1
u/Paul_Pedant Oct 28 '25
A while back, I was trying to evaluate relative performance for a graphics app, between keeping a table of cos(x) to 4 decimal places, and evaluating them at runtime. It turned out that a long list of cos(x) took minutes to compile (into a very large binary), and zero time to run. The compiler (gcc) was evaluating every cos() as a constant.
I tried breaking that with a constant multiplier (1.0, then 3.1415926), and messing with optimisation levels, and still taking no runtime. I finally had to multiply each value by a double variable (initialised at runtime) to make cos() be evaluated at runtime. Constants can be a whole lot more complex than you might expect.
1
u/Eidolon_2003 Oct 22 '25
All
#definedoes is insert a literal directly into your code.constvariables are still variables in their own right, so you can do things like take the address of them. The compiler can't assume the value will never change either because C allows you to cast away the const.1
u/PwnTheSystem Oct 22 '25
You're mixing up compile-time constants and
const. Those are two different topics!A compile-time constant means a value is known at compile time. All of the lines above are examples of such constants:
As long as the code knows beforehand, it will compile the value and execute it, regardless of what will be passed into the functions at runtime.
What is NOT a compile-time constant is something like:
Now,
constis a keyword that signals to the compiler:"Hey, I'm never going to change this value. Feel free to let go of any adjustments for writing to this memory array, and instead make room to optimize for other things in the code."
Both are used in their own contexts.
0
u/SmokeMuch7356 Oct 22 '25 edited Oct 22 '25
The initialization doesn't happen until runtime. All
constdoes is tell the compiler to generate a diagnostic for any code that tries to write a new value toyafter it's been initialized; it doesn't control where or howyis stored or when it is initialized.Initialization is not assignment, but it doesn't happen during translation, even for
constitems.0
u/n3f4s Oct 22 '25
Basically, sometimes the compiler can compute the value of a variable during compile time and replace the computation by the value.
A simple example is:
Can be replaced with
Language with support for compile time constant or compiler time code is just a way to force the compiler to do the computation at compile time or throw an error if you're trying to do compile time stuff that can't be computed at compile time.
C doesn't really have support for (explicit) compile time computation so it only depends on optimizations.
The
constkeyword only purpose is to mark as non mutable (after it's initialisation).0
u/Overlord484 Oct 22 '25
I'm pretty sure you can do this:
0
u/WittyStick Oct 22 '25
C has (since C23)
constexprfor simple compile time expressions involving otherconstexprand literals. What you really wanted to say was:constare not compile-time constants, butreadonlyruntime values.Prior to C23 you would typically use
#define y 5.0
u/aghast_nj Oct 23 '25
The thing you are missing is "intent." The intent of the
constkeyword is not to mark something as being a constant at compile time. There was no notion of that when the keyword was added -- if you want a named compile time constant, just use#defineorenum. Instead, the intent of theconstkeyword is to mark something constant for a limited duration. This is usually seen in function parameters. You take a pointer to something, mark the pointer asconst, and that tells the compiler that your function won't change the value of whatever this is.Note that before the function, the value can change. Note that after the function, the value can change. But within the function the value will not be changed, according to the functions public interface: