Directive Syntax | Directive Description |
---|---|
File inclusion |
|
replace directive with file from C library path | |
replace directive with file from source path | |
Macro definition |
|
replace every occurrence of name with text | |
replace every occurrence of name with argument with text | |
suspends macro substitution | |
Conditional compilation |
|
if expression is true compile section | |
if name is defined compile section | |
if name is not defined compile section | |
else if expression is true compile section | |
else compile section | |
terminates #if block | |
continue definition in next line | |
parameters concatenation | |
Other directives |
|
#pragma token |
do compiler dependent action |
#error token |
write diagnostic message |
comment | |
line comment (C++ feature - not ANSI C) |
#include |
Syntax: |
#include <filename.h> #include "filename.h" |
Description: |
Include header file instead of directive. |
Example: |
#include <stdio.h> #include "my_defs.h"
int main() { printf("Example"); } |
#define |
Syntax: |
#define name text #define name(args) text |
Description: |
Replace every occurrence of name with text |
Example: |
#define DEBUG #define C 3.0e8 #define abs(X) ((X)>0 ? (X) : -(X) |
#undef |
Syntax: |
#undef name |
Description: |
Suspends macro substitution of name |
Example: |
#undef C #undef abs |
#if |
Syntax: |
#if name |
Description: |
If expression is true compile section |
Example: |
#define DEBUG 1 #if DEBUG : code will be compiled when DEBUG is 1 : #endif |
#ifdef |
Syntax: |
#ifdef name |
Description: |
If name is defined compile section |
Example: |
#define DEBUG #ifdef DEBUG : code will be compiled when DEBUG is defined : #endif |
#ifndef |
Syntax: |
#ifndef name |
Description: |
If name is not defined compile section |
Example: |
#define VER2 #ifndef VER2 : code will be compiled when VER2 is not defined : #endif |
#elif |
Syntax: |
#if name : #elif name : #endif |
Description: |
Else if expression is true compile section |
Example: |
#define VER1 0 #define VER2 1 #if VER1 : //code will not be compiled #elif VER2 : //code will be compiled #endif |
#else |
Syntax: |
#if name : #else : #endif |
Description: |
Else compile section |
Example: |
#define VER 0 #if VER : //code will not be compiled #else : //code will be compiled #endif |
#endif |
Syntax: |
#if name : #elseif name : #else : #endif |
Description: |
Terminates #if block |
Example: |
#if VER : #endif |
\ |
Syntax: |
#define name text \ text \ text |
Description: |
Continue definition in next line |
Example: |
#define X { {1,2}, \ {2,4}, \ {3,6} } |
## |
Syntax: |
#define name text(arg) arg##text |
Description: |
Continue definition in next line |
Example: |
#define con(X,Y) X##Y |
/* */ |
Syntax: |
/* text */ |
Description: |
Text comment |
Example: |
/* This is a comment */
/* and also this is a comment */ |
// |
Syntax: |
// text |
Description: |
Line comment (non ANSI C - C++ feature) |
Example: |
// This is a comment // and also this is a comment |