0

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 ?

harald
  • 5,976
  • 1
  • 24
  • 41
bc247
  • 17
  • 1
  • 1
  • 6

2 Answers2

1

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).

Brendan
  • 35,656
  • 2
  • 39
  • 66
0

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.

Ignited
  • 771
  • 3
  • 7
  • 21