Preprocessor Directives


    • The C pre-processor is not the part of the compiler, but is a separate step in the compilation process.
    • The syntax of the pre-processor is different from the syntax of the rest of C program in several respects.


    • The pre-processor is a translation phase that is applied to the source code/program, before the compiler gets its hands on it.
    • C pre-processor is just a text substitution tool, which filters our source code before it is compiled.


    • The pre-processor more or less provides its own language, which can be a very powerful tool for the programmer.
    • All pre-processor directives or commands begin in C with the symbol #.

Types of Pre-processor Directives

    • The pre-processor directives performs textual substitutions during the compilation time in our source code. On this basis, they are of three types –
          (i) File Inclusion: Inserting the contents of another file into our source file, as if we
               had typed it all in there.
          (ii) Macro Substitution: Replacing instances of one piece of text with another.
          (iii) Conditional Compilation: Depending on various circumstances, certain  parts of the source code are seen or not seen by the compiler at all.


    • The preprocessor makes programs easier to develop, read and modify.
    • The preprocessor makes C code portable between different machine architectures & customizes the language.
    • The preprocessor allows us to customize the language.

Lists of Pre-processor Directives

      • #define is used to define constants.
      • #define is also used to create functional macros.
      • No semicolon (;) need to be placed as the delimiter at the end of a # define line.
      • Syntax is –
           # define <literal> <replacement-value>   OR
     # define <constant-name> <replacement-value>
       (Where literal is identifier which is replaced with replacement-value in the                          program.)
      • For Example –
           #define MAXSIZE 20
           #define PI 3.141

(Here, the C preprocessor simply searches through the C code before it is compiled and replaces every instance of MAXSIZE and PI with 20 and 3.141 respectively.)

      • In #define preprocessor, the literal is substituted by literal value/constant at every occurrence, before compilation of the program. Since the values of the literal are constant throughout the program, they are called as constant.




    • A macro is a segment of code which is replaced by the value of defined macro.
    • A macro is a segment of code which is replaced by the value of macro.Macros are string replacements technique commonly used to define constants.
    • Macros are symbolic constants and compile time functions.
    • Basically macros are one of the Pre-processor directives available in C.


The macro definition takes the following form :
#define identifier string
    • Here, #define is a type of macro/Pre-processor directives, the identifier is  a macro name and the string as macro replacement.
    • To create/define a macro :
      • The above syntax statement is included in the program at the beginning(above main() with other header files), then the pre-processor replaces every occurrence of the identifier in the source code by the string value automatically.
      • To create a macro, the keyword #define is written followed by the identifier name and a string value with at least one blank space between them (as mention in syntax).
      • The definition is macro is not terminated by semicolon(;) symbol as line terminator.
      • The string value may be any constant/expression while the identifier name must be a valid C name in capital letters.
      • We can also use macro defined in the system standard libraries such as  ‘abs’, which returns the absolute value of its parameter. Again, we can also create/define our own version of customized ABS macro similar like system defined macro but with different identifier name such as ABS (System defined is abs) to avoid conflicting with the existing name abs in system standard library.

#define ABS(x)  ((x) < 0) ? – (x) : (x)

    • To Disable/Undefining a Macro :
      • A defined macro can be disable/undefined ,using #undef statement.
      • Syntax :

#undef identifier(name)

      • This option is applied when we want to restrict the definition of macro only to a particular region/part of the program.


#define MAX 200

It sets the value of identifier MAX to 200 and whenever we want to use 200 in our program we can put MAX instead of writing 200 which are later substituted by the compiler at proper place with its value/constant. In another words, Whenever we want to use value of MAX  in our program segment code we can simply write MAX instead of writing lengthy value of MAX again and again. We can put/use multiple times MAX in our program code as per need of the program.


    • It is a pre-defined symbol with or without parameters.
    • We can create our own macros with the #define pre-processor directive.
    • When we use the macro, its value (with the actual parameters) are substituted at the location in the program where we use it.
    • We can create our own customized macro as per logic/need of the program or can also use system defined standard library macro in our program.
    • Macros are inline code, which are substituted at compile time.
    • All the macros are replaced with their definitions during compile time.
    • Macro substitution is a process where an identifier in a program is replaced by a predefined string by the pre-processor.
    • Macros can also accept parameters and return values. These macros are called Macro Functions.
    • The Main use of Macro is not like to solve this type of program rather it is often used to create codes which can be compiled on different machines.
    • Macros work similar like as a function but with little differences
      • Macros are implemented as a textual substitution, therefore by this the performance of program improves compared to functions
      • Recursive macros are generally not to suggest to use like functions.
      • Macros don’t care about the data type of their arguments. Hence macros are a good choice where we want to operate on reals, integers or a mixture of the two.
      • Macros are generally fairly small.

      Types of Macro Substitutions

        • There are different types of macro substitution.These are:-

      (a) Simple Macro Substitution :

          • They are simple string replacements with defined identifier.
          • They are also called ‘Object like Macro’.
          • They are widely used macro.
          • For example – 
        #define VAL  70
        #define MAX 200

        (b) Argumented Macro Substitution :

            • This type of Macro gives us to define more complex and more useful forms of replacements.
            • They are also called ‘Function like Macro’ because they work like as function call.
            • Syntax :

        #define identifier(f1,f2,..,fn) string

            • For example
        #define CUBE(x) (x*x*x)
        #define SQUARE(Y) (Y*Y)
        The above macro statement can be used in the program as following  format –
        then the pre-processor will expand the defined macro in the program at proper place as –

        (c) Nested Macro Substitution :

            • Here, we can use one/more macro in the definition of another macro as per logic/need of the program.
            • For example –
        #define SQUARE(x) (x*x)
        #define CUBE(SQUARE(x)*(x))


        #define VAL 10
        #define NVAL (VAL +1)
        #define MAX(VAL , NVAL )  ((VAL >NVAL ) ? VAL :NVAL )


          • Macros can make long, ungainly pieces of code into short words.
          • It increases the execution speed of the code because there is no function call overhead.
          • They are used to avoid repetitive codes and some dependencies in the program.


          • Macro is used carefully, taking in mind the order of precedence of operators specially arithmetic type.
          • Macros don’t show debugger results correctly, or are less clear.


        Categories: C


        Leave a Reply

        Your email address will not be published. Required fields are marked *

        This site uses Akismet to reduce spam. Learn how your comment data is processed.