Preprocessor Directives

In a C program, all lines that start with # are processed by preprocessor which is a special program invoked by the compiler. In a very basic term, preprocessor takes a C program and produces another C program without any #.


There are 4 main types of preprocessor directives: 
  • Macros
  • File Inclusion
  • Conditional Compilation
  • Other directives
All preprocessor commands begin with a hash symbol (#). It must be the first nonblank character, and for readability, a preprocessor directive should begin in the first column. The following section lists down all the important preprocessor directives

Sr.No.Directive & 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.

11

 

#pragma

Issues special commands to the compiler, using a standardized method.

 
Following are some interesting facts about preprocessors in C.

1) When we use include directive,  the contents of included header file (after preprocessing) are copied to the current file.
Angular brackets < and > instruct the preprocessor to look in the standard folder where all header files are held.  Double quotes  and  instruct the preprocessor to look into the current folder and if the file is not present in current folder, then in standard folder of all header files.
Eg:
#include <math.h>
#include "yourheaderfile.h"

2) When we use define for a constant, the preprocessor produces a C program where the defined constant is searched and matching tokens are replaced with the given expression. 
 
For example in the following program max is defined as 100.

#include<stdio.h>
#define max 100
int main()
{
printf("max is %d", max);
return 0;
}
Output: 
max is 100
 
Note that the max inside "" is not replaced

A macro can also be defined using #define.A macro is a segment of code which is replaced by the value of macro. Macro is defined by #define directive

#include <stdio.h>
#define MULTIPLY(a, b) a*b
int main()
{

printf("%d", MULTIPLY(2, 3));
return 0;
}
 
3)Preprocessors also support if-else directives which are typically used for conditional compilation.
 
#ifndef MESSAGE 
#define MESSAGE "You wish!" 
#endif

4)A header file may be included more than one time directly or indirectly, this leads to problems of re declaration of same variables/functions. To avoid this problem, directives like definedifdef and ifndef are used.

Most compilers targeting Microsoft Windows implicitly define _WIN32.This allows code, including preprocessor commands, to compile only when targeting Windows systems. A few compilers define WIN32 instead. For such compilers that do not implicitly define the _WIN32 macro, it can be specified on the compiler's command line, using -D_WIN32.
 
#ifdef __unix__ //__unix__ is usually defined by compilers targeting Unix systems
# include <unistd.h> 
#elif defined _WIN32     /* _WIN32 is usually defined by compilers targeting 32 or                                             64 bit Windows systems */ 
# include <windows.h> 
#endif 

5)There are some standard macros which can be used to print program file name (__FILE__), Date of compilation (__DATE__), Time of compilation (__TIME__) and Line Number in C code (__LINE__) 
 
#include <stdio.h>
int main()
{
printf("Current File :%s\n", __FILE__ );
printf("Current Date :%s\n", __DATE__ );
printf("Current Time :%s\n", __TIME__ );
printf("Line Number :%d\n", __LINE__ );
return 0;
}

Comments

Popular posts from this blog

Programming in C GXEST204 - KTU 2024 scheme syllabus notes pdf ------- Dr Binu V P

Structure of a C Program

Files in C