Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
IEC 1539-3-02, which is identical to ISO/IEC 1539-3:2002 and its Canadian adoption CAN/CSA-ISO/IEC 1539-3:02, defines a standardized conditional compilation facility for the Fortran programming language. This facility enables Fortran source code to be selectively compiled based on conditions evaluated during program translation. Unlike external preprocessors (e.g., the C preprocessor) that operate on the raw source text, this standard embeds conditional compilation directives directly into the Fortran source form, using specially interpreted comment lines. The primary goal is to improve portability across different platforms and compilation environments, reduce reliance on implementation-specific preprocessor flags, and provide a language-consistent mechanism for managing debug code, platform-specific features, and optional functionality without harming readability or maintainability.
The standard applies to both free source form and fixed source form Fortran. It is intended to be used in conjunction with any revision of Fortran that supports the syntax and semantics described in ISO/IEC 1539-1 (the core Fortran language). The facility is designed so that processors that do not implement the conditional compilation extensions treat the directive lines as ordinary Fortran comments, thus preserving backward compatibility. This scope ensures that code written with #comment directives can be presented to any Fortran processor as valid text, and only those that recognize the directives will activate conditional compilation.
Conditional compilation directives are introduced by the distinctive character sequence !? — an exclamation mark followed immediately by a question mark. In fixed source form, the sequence may appear in any column, but it must be the first non-blank characters of the line for the line to be recognized as a directive. The directive keywords (IF, THEN, ELSE, ELSE IF, END IF) follow the Fortran identifier rules and are case-insensitive. A condition expression, enclosed in parentheses, appears after the IF or ELSE IF keyword. The standard defines four types of directive lines:
!? if (expression) then or !? else if (expression) then!? else!? end if!? (including continuation lines) and is not one of the above directive forms. CCLs are subject to the condition of the enclosing block.Condition expressions can be as simple as a single identifier or as complex as a boolean expression combining multiple identifiers and integer constants. The defined operator returns .TRUE. if the identifier has been defined (typically via a compiler command-line option). Relational and logical operators from Fortran are used: .EQ., .NE., .LT., .LE., .GT., .GE., .AND., .OR., .NOT., .EQV., .NEQV.. The standard mandates short-circuit evaluation: once the overall truth value is determined, remaining sub-expressions are not evaluated. The following table summarises operator precedence (from highest to lowest):
| Level | Operators |
|---|---|
| 1 (highest) | defined, .NOT. |
| 2 | .EQ., .NE., .LT., .LE., .GT., .GE. |
| 3 | .AND. |
| 4 | .OR. |
| 5 | .EQV., .NEQV. |
The standard does not mandate any predefined identifiers. Instead, it defines an identifier as a name that may be defined externally to the source program (e.g., via a command-line flag). A processor may also predefine implementation-specific identifiers, but such identifiers must be documented and should not conflict with the user’s namespace. This design avoids portability pitfalls while allowing implementors to supply useful built-in symbols (e.g., identifying the operating system or compiler version). The article is implementation-defined but must follow the same syntax rules.
Note: The conditional compilation directive syntax !? is distinct from the !$ OpenMP sentinel. The two are independent; however, a CCL starting with !?$ could be used to conditionally introduce OpenMP directives, but this is not standardized.
For a processor to conform to IEC 1539-3-02, it must recognize !? directives during the first stage of program translation, before lexical analysis of the Fortran source text. The processor maintains a stack of nested conditional blocks and evaluates condition expressions using identifiers defined externally (typically via compiler options such as -D). Identifiers can be defined as either a flag (no value) or a constant integer value; the defined operator only checks for existence, while the identifier itself can be used as an integer constant in comparisons. The standard does not support parameterized macro expansion or string substitution.
Conforming implementations must correctly handle nesting up to a depth limited by the processor but should support at least 32 levels. The conditional block structure must be properly terminated; missing !? end if should cause an error. The overall effect is that the compiler behaves as if the inactive lines (both CCLs and ordinary lines excluded by a false condition) are not present in the source stream.
Implementation challenge: Because directives are removed before lexical analysis, they cannot be used to change Fortran token boundaries or to conditionally include part of a statement spanning multiple lines. Each line is either entirely present or absent.
A processor claiming compliance with IEC 1539-3-02 must implement all mandatory features of the standard, including the !? directive syntax, condition expression evaluation, and identifier definition mechanism. The standard does not require a processor to provide any predefined identifiers; nevertheless, a conforming processor must document a way for users to supply definitions. Test suites (such as those originally developed for Fortran 90 conditional compilation verification) can be used to validate compliance.
Since IEC 1539-3-02 is an optional part of the ISO/IEC 1539 series, compiler vendors may choose not to implement it. However, several major compilers (including Intel Fortran with the /fpp:lines=!? option and NAG Fortran) provide support. For projects that must run across a broad range of compilers, it is recommended to supplement conditional compilation directives with equivalent fallback mechanisms — for example, using logical constants initialized at compile time or relying on a separate preprocessing step. The following tips can help maximize code portability:
!? if defined(IDENTIFIER) then rather than numeric comparisons when only presence of the identifier matters.Caution: Some compilers ignore !? lines without error, treating them as comments. This will silently produce the false‑branch behaviour (all CCLs are ignored). Always test your configuration systematically.
!? directives allowed in fixed source form?!? sequence can start in any column. However, the directive keyword and condition must be contained within the same line. As with any directive, the line is treated as a comment by processors that do not implement the standard.!? directives is separate from the Fortran namespace. The directive identifiers are only evaluated at compile‑time and have no connection to variables of the same name within the program. However, to avoid confusion, many projects adopt a naming convention such as using uppercase names with leading underscores for conditional identifiers.Standard reference: IEC 1539-3-02 (CAN/CSA‑ISO/IEC 1539‑3:02) | Article prepared in 2026.