Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are constant C expressions evaluated at compile time or at runtime?

If I write a #define that performs an operation using other preprocessor constants, is the final value computed each time the macro appears at runtime? Does this depend on optimizations in the compiler, or is it covered under a standard?

Example:

#define EXTERNAL_CLOCK_FREQUENCY    32768 #define TIMER_1_S                   EXTERNAL_CLOCK_FREQUENCY #define TIMER_100_MS                TIMERB_1_S / 10 

Will the operation 32768 / 10 occur at runtime every time I use the TIMER_100_MS macro?

I would like to avoid the following:

#define EXTERNAL_CLOCK_FREQUENCY    32768 #define TIMER_1_S                   EXTERNAL_CLOCK_FREQUENCY #define TIMER_100_MS                3276 

Summary

A compiler is required to be able to evaluate constant integral expressions because they are necessary for calculating things like array sizes at compile time. However, the standard only says they "can" -- not "must" -- do so. Therefore, only a brain-dead compiler would not evaluate a constant integral expressions at compile time, but a simple check of the assembly output for an unconventional compiler would verify each case.

like image 246
Judge Maygarden Avatar asked Jan 12 '09 17:01

Judge Maygarden


People also ask

What is compile time constant in C?

A compile-time constant is a value that can be (and is) computed at compile-time. A runtime constant is a value that is computed only while the program is running. If you run the same program more than once: A compile-time constant will have the same value each time the application is run.

What is constant expression in C++?

A constant value is one that doesn't change. C++ provides two keywords to enable you to express the intent that an object is not intended to be modified, and to enforce that intent. C++ requires constant expressions — expressions that evaluate to a constant — for declarations of: Array bounds.

What is meant by constant expression?

A constant expression is an expression that can be evaluated at compile time. Constants of integral or enumerated type are required in several different situations, such as array bounds, enumerator values, and case labels. Null pointer constants are a special case of integral constants.

Why does array size need to be known at compile time?

If you create it as a local variable, and specify a length, then it matters because the compiler needs to know how much space to allocate on the stack for the elements of the array. If you don't specify a size of the array, then it doesn't know how much space to set aside for the array elements.


1 Answers

Macros are simply textual substitution, so in your example writing TIMER_100_MS in a program is a fancy way of writing 32768 / 10.

Therefore, the question is when the compiler would evaluate 32768 / 10, which is a constant integral expression. I don't think the standard requires any particular behavior here (since run-time and compile-time evaluation is indistinguishable in effect), but any halfway decent compiler will evaluate it at compile time.

like image 163
David Thornley Avatar answered Oct 04 '22 13:10

David Thornley