Skip to main content

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 defined, ifdef 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

KTU Mandatory C programs for Laboratory and Solutions

LIST OF LAB EXPERIMENTS 1. Familiarization of Hardware Components of a Computer 2. Familiarization of Linux environment – Programming in C with Linux 3. Familiarization of console I/O and operators in C     i) Display “Hello World”     ii) Read two numbers, add them and display their sum     iii) Read the radius of a circle, calculate its area and display it 4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution. Read the values of the variables from the user through console 5. Read 3 integer values, find the largest among them. 6. Read a Natural Number and check whether the number is prime or not 7. Read a Natural Number and check whether the number is Armstrong or not 8. Read n integers, store them in an array and find their sum and average 9. Read n integers, store them in an array and search for an element in the    array using an algorithm for Linear Search 10.Read n integers, store them in an array and sort the elements in t

PROGRAMMING IN C KTU EST 102 THEORY AND LAB NOTES

PROGRAMMING IN C  KTU  EST 102  THEORY AND LAB   COMMON FOR ALL BRANCHES About Me Syllabus Theory Syllabus Lab Model Question Paper EST 102 Programmin in C University Question Papers  and evaluation scheme   EST 102 Programming in C  Introduction( Lab) Introduction to C programming Linux History and GNU How to create a bootable ubuntu USB stick Installing  Linux Install Linux within  Windows Virtual Box and WSL Linux Basic Features and Architecture Basic Linux Commands Beginning C Programming Compiling C programs using gcc in Linux Debugging C program using gdb Module 1: Basics of computer hardware and software          Module-1 Reading Material Basics of Computer Architecture Hardware and Software System Software and Application Software  Programming Languages ( High level, Low level and Machine Language) and Translators ( Compiler, Interpreter, Assembler) Algorithm, Flowcharts and Pseudo code Program Development Structured Programming Basics of hardware ( video) Know about Motherboar

Arrays in C-single and multi dimensional arrays- list and matrix

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list(vector); A two dimensional array is like a table(matrix). We can have more dimensions. Always, Contiguous (adjacent) memory locations are used to store array elements in memory. Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element. Declaring  Single Dimensional Arrays Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for mentioning dimension of the array.  Dimensions used when declaring arrays in C must be positive integral constants or constant expressions.  In C99, dimensions must still be positive integers, but variables can be used, so long as the variable has a positive value at the time the array is declared. ( Space is allo