What is the definition of compile time and run time operator in C ? I know sizeof() is a compile time operator in C but which are the run time operators in C ?
-
Anything that depends on the value of the operand, like `+` and `*`. – Barmar May 11 '15 at 10:10
-
@LPs The answers there don't seem to be about operators. – Barmar May 11 '15 at 10:12
-
note: it's easier to list the ones which are evaluated at compile time... – Karoly Horvath May 11 '15 at 10:12
-
2Please choose a language, and tag accordingly. Are you specifically asking about C? The answer would be rather different for C++, where more operations can be done at compile time. – Mike Seymour May 11 '15 at 10:12
-
2`sizeof` can be evaluated at runtime in C (>= C99) – Mat May 11 '15 at 10:12
-
1Oh great cheers for that – Lightness Races in Orbit May 11 '15 at 10:20
-
1http://stackoverflow.com/q/30165196/560648 – Lightness Races in Orbit May 11 '15 at 10:22
-
wrong assumption, if the argument is a VLA, `sizeof` can only be calculated at run time – Jens Gustedt May 11 '15 at 11:38
2 Answers
For C; various optimisations (e.g. constant propagation and constant folding) mean that every operator can potentially be done at compile time (if the situation permits it).
For a simple example, consider this function:
int foo(int a, int b) {
return a+b;
}
This looks like the addition needs to be done at run time.
Now consider this code:
int x = foo(1, 2);
Now the compiler can "inline" the function, propagate the constants, and do the addition at compile time. You'd end up with int x = 3;
(plus a potentially redundant copy of the foo()
function which is capable of doing the addition at run time).
There are also cases where an optimisation can't be done at compile time but can be done during linking (with LTO/Link Time Optimisation); where the operator isn't evaluated at compile time or run time. A simple example would be if code in another object file ("compilation unit") did int x = foo(1, 2);
.
Also, in general, the opposite is also true - nothing guarantees that an operator will be evaluated at compile time when it is possible; so you can't say that an operator is always "compile time". For a simple example of this, consider string concatenation (e.g. char *myString = "Hello " "World!";
) - it would be legal for the compiler to generate code that does the concatenation at run time (even though it's hard to think of a reason why a compiler would want to).

- 35,656
- 2
- 39
- 66
Compile time operators -> Calculated during compilation
Run time operators -> During execution
Example:
INPUT A,B
C = A + B
Here + is run time operator as it depends on values you input.

- 771
- 3
- 7
- 21