marack
marack

Reputation: 2066

Is __TIME__ preprocessor macro guaranteed to be constant within a file?

Just out of curiosity I am wondering whether the value given by the standard __TIME__ preprocessor macro can change within a single translation unit?

In other words, is __TIME__ determined once during preprocessing and then fixed, or is it re-evaluated each time it is encountered?

If this is not specified by the C standard, is there a de facto standard behavior among the major implementations (gnu, clang, intel, msvc)?

Upvotes: 9

Views: 630

Answers (1)

chux
chux

Reputation: 153640

C does not rigorously specify the the " time of translation" is constant throughout a pre-processing/compilation.

My experience has been it is constant. But since it is only to the second, a given compilation would need to cross a second boundary to differ.

__TIME__ The time of translation of the preprocessing translation unit: a character string literal of the form "hh:mm:ss" as in the time generated by the asctime function. If the time of translation is not available, an implementation-defined valid time shall be supplied. C11 §6.10.8.1 1

The definitions for __DATE__ and __TIME__ when respectively, the date and time of translation are not available (6.10.8.1). (Informative) C11 §J.3.11 1

Upvotes: 5

Related Questions