C Reference - Preprocessor directives

Directive Syntax Directive Description
File inclusion
 

#include <filename.h>

replace directive with file from C library path

#include "filename.h"

replace directive with file from source path
Macro definition
 

#define name text

replace every occurrence of name with text

#define name(args) text

replace every occurrence of name with argument with text

#undef name

suspends macro substitution
Conditional compilation
 

#if expression

if expression is true compile section

#ifdef name

#if defined(name)

if name is defined compile section

#ifndef name

#if !defined(name)

if name is not defined compile section

#elif

else if expression is true compile section

#else

else compile section

#endif

terminates #if block

continue definition in next line

##

parameters concatenation
Other directives
 

#pragma token

do compiler dependent action

#error token

write diagnostic message

/* text */

comment

// text

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

 

Write how to improve this page

C REFERENCE
RAPID TABLES