Menu Close


C: Compiler directives

Compilation process is divided in two parts. First part is reading and is reacting to compiler directives. These are special instructions that help the compiler to prepare the source code for compilation. All compiler directives start with symbol “#”.




create a macro


remove a macro


Inserts a header file


Check if a macro is defined


Check if a macro is not defined


Check a compile time condition


The alternative path for #if directive


Ladder directive, used with #if 


Ends conditional directive


Prints error message on stderr


Special compiler directives


C is a modular language. You can combine smaller files into a larger program using #include directive followed by the header file name. There are two kind of header files: System headers and project local headers.

The C 90 standard headers list:
















Headers added since C 90:










Project header files:

You can create your own header files and store them with your program files. A good practice is to store in header files: constants, macros, system wide global variables, and function prototypes.

Note: Function prototype is the function signature without its body. Functions can be implemented in  other *.c source files that do not have same name as the header file. 


Constants: Using #define you can create constants. These constants must use uppercase names.

Macros: Using #define you can create macros. These are expressions or reusable fragments of code:

Note: AVG in previous example looks like a function but is not. It is a “macro” so it uses uppercase letters for identifier. The real difference is that macros are converted into literal expressions before compilation. Since this is not a function call, macro parameters do not have types, they are text literals that are replaced in expressions before compilation.

Warning: There is a danger that macro can produce unexpected results if is not enclosed in extra parentheses. It is a bad practice to overuse macros. It may decrease program readability and maintainability.


This directive stop the compilation and display a message defined after #error:


This directive do not stop the compilation but display a warning message:


This is a conditional compiler directive. It uses a logical expression to make a decision block executed before compilation. The conditional expression can contain normal relation and logic operators in addition it can use operator defined() that take as argument a defined constant or macro.

Predefined macros

ANSI C defines some useful macros and variables:

__FILE__ => The name of the current file, as a string literal
__LINE__ => Current line of the source file, as a numeric literal
__DATE__ => Current system date, as a string
__TIME__ => Current system time, as a string

Note: Some compilers have special #pragma directives and other operators. For more details about processor directives you must consult the compiler specific documentation. These details are out of scope from my introductory tutorial.

Next article: Memory management