C preprocessor

INTRODUCTION TO C PREPROCESSOR

0  0 2016-11-03 11:08:50

It is not a part of compiler. In compilation process, it is a separate step in C language.
It is test substitution tool. It give instruction to compiler to do follow required pre-processing before the actual compilation. We can refer as CPP.
All CPP commands use # symbol in the start.

Following list of CPP commands:
  Directive            Description    
• #define            ->      Substitutes a pre-processor macro
• #includ            ->      insert a header file
• #undef             ->     Undefined a pre-processor macro
• #ifdef              ->      if macro defined then return true
• #ifndef            ->     if macro is undefined then return false
• #if                  ->     check condition is true
• #else             ->     alternate of true condition
• #elif               ->     #else and #if  both are in one statement
• #error            ->     error occurred
• #pragma        ->     Issues special commands to the compiler, using a standardize method

Pre-processors Examples
#define MAX_ARRAYS_LENGTH 30:
This statements tells CPPN to MAX_ARRAYS_LENGTH is 30. It is constant value in a program.

#include <stdio.h>
#include "myfileheader.h"
These directives tell the CPP to get stdio.h from System standard Libraries and add the text to the current source file. The next line tells CPP to get myfoleheader.h from the local directory and add the content to the current source file.
#undef  FILE_SIZE
#define FILE_SIZE 50
It tells the CPP to undefine existing MYFILE_SIZE and define it as 50
#ifndef MESSAGE
#define MESSAGE "Hello World"
#endif
It tells the CPP to define MESSAGE only if MESSAGE isn't already defined.
#ifdef DEBUG
   /* Your debugging statements here */
#endif
It tells the CPP to process the statements enclosed if DEBUG is defined. This is useful if you pass the -DDEBUG flag to the gcc compiler at the time of compilation. This will define DEBUG, so you can turn debugging on and off on the fly during compilation.
Predefined Macros :
There are predefined Marco available. They are following
__DATE__   -     It takes current date in MM DD YYYY format
__TIME__     -     it takes current time in HH:MM:SS format
__FILE__      -     it contains current filename as a string literal.
__LINE__      -    it contains current line number as decimal constant
__STDC__   -   defined 1 when compiler complies with the ANSI standard.

Example −
#include <stdio.h>
main()
{
    printf("current File Name :%s\n", __FILE__ );
    printf("current Date :%s\n", __DATE__ );
    printf("current Time  :%s\n", __TIME__ );
    printf(" no of Line :%d\n", __LINE__ );
    printf("compiler complies ANSI :%d\n", __STDC__ );
}

Out put:
current File Name :macro.c
current Date:July 8 2016
current Time :02:02:02
no of Line:8
compiler complies ANSI :1

 

 

Comments (0)

Post Your Comments

Headquarter



Hong Kong Office



Find Us On

Website Development Company
SEO
Digital Marketing
website development company in Hong Kong
website designer in pune