Macros and preprocessor

Macros and C Preprocessor

Macros

A macro is a fragment of code that is given a name. You can define a macro using the #define preprocessor directive.

There are two types of macros :

  1. Object-like Macros is an identifier that is replaced by value. It is widely used to represent numeric constants.

    Example :- #define PI  3.14   

  // PI is the macro name which will be replaced by the value 3.14.

  1. Function-like Macros looks like function call.

    Example :-  #define MIN(a,b) ((a)<(b)?(a):(b))

  // MIN is the macro name.   

C Preprocessor

The C Preprocessor is not a part of the compiler, but is a separate step in the compilation process. In simple words, Preprocessor is just a text substitution tool and it instructs the compiler to do required pre-processing before the actual compilation. All preprocessor directives starts with hash # symbol.

Some preprocessor directives is given below :

S No.

Directives 

                          Description

1.

#define

Substitutes a preprocessor macro.

2.

#include

Inserts a particular header from another file.

3.

#undef

Undefines a preprocessor macro.

4.

#ifdef

Returns true if this macro is defined.

5.

#ifndef

Returns true if this macro is not defined.

6.

#if

Tests if a compile time condition is true.

7.

#else

The alternative for #if.

8.

#elif

#else and #if in one statement.

9.

#endif

Ends preprocessor conditional.

10.

#error

Prints error message on stderr.

 
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free