Home C Programming Tutorial Preprocessors in C

Preprocessors in C

by Techarge
81 minutes read

In this tutorial, you’ll learn about Preprocessors in C. You will learn how to define and use C Preprocessor with the help of examples.

  • The preprocessor includes the instructions for the compiler, these instructions are executed before the source code is compiled.
  • Preprocessor directives begin with a hash sign(#).
  • No semicolon (;) is expected at the end of a preprocessor directive.

C provides the following preprocessor directives:

The #define Preprocessor Directive as Constant

The #define preprocessor directive is used to define constant values in the program.

Syntax of #define preprocessor directive

 #define macro_name value

The #define preprocessor directive define the macro-name and value, this value is replaced with each time the macro-name appears in the program .

Examples of #define preprocessor directive

EXAMPLE 1

#include<stdio.h>
#define PI 3.14
void
main ()
{
  int r;
  float area;

  printf ("Enter radius : ");
  scanf ("%d", &r);

  area = PI * r * r;

  printf ("\nArea of circle is : %f", area);
}

OUTPUT

Enter radius : 5
Area of circle is : 78.5

In the above example PI is macro-name.

EXAMPLE 2

#include<stdio.h>
#define SIZE 5
void
main ()
{
  int arr[SIZE];


  for (int i = 0; i < SIZE; i++)
	{
	  printf ("Enter any number : ");
	  scanf ("%d", &arr[i]);
	}

  for (i = 0; i < SIZE; i++)
	printf ("%d, ", arr[i]);

}

Output :

Enter any number : 78
Enter any number : 45
Enter any number : 12
Enter any number : 89
Enter any number : 56
78, 45, 12, 89, 56,

In the above example SIZE is macro-name.

The #define Preprocessor Directive as Function

The #define preprocessor directive can be used as inline function which is replaced at compile time. The definition of macro accepts an argument and that argument is replaced by the actual argument found in program.

Example of #define preprocessor as function

EXAMPLE

#include<stdio.h>
#define SQUARE(x)       x*x
void
main ()
{
  int num;


  printf ("Enter any number : ");
  scanf ("%d", &num);

  printf ("\nThe square is : %d", SQUARE (num));

}

Output :

Enter any number : 5
The square is : 25

Difference between Macros and functions

MacrosFunctions
Macro calls are replaced with macro expression.In function call, the control is passed to a function definition along with arguments,and definition is processed and value may be returned to call.
Macros run programs faster but increase the program size.Functions run programs slower but decrease the program size and compact.
It is better to use Macros, when definition is very small in size.It is better to use Functions, when definition is bigger in size.
Difference between Macros and functions

The #include Preprocessor Directive

The #include preprocessor directive is used to include another source file in our source code. This is commonly used to include header files in our program.

Syntax of #include preprocessor directive

#include "filename.h"
or
#include <filename.h>

The source file to be added must be enclosed within double quotes (” “) or angle brackets (< >) . If the file is enclosed in double quotes the search will be done in current directory. If the file is enclosed in angle brackets the search will be done in standard directories (include directory) where the libraries are stored.

The #if-#else-#endif Preprocessor Directive

The #if preprocessor directive takes condition in parenthesis, if condition is true, then the statements given between #if and #else will get execute. If condition is false, then statements given between #else and #endif will get execute.

Syntax of #if-#else-#endif Preprocessor Directive

#if(condition)
- - - - - - - - - - -
- - - - - - - - -
#else
- - - - - - - - - - -
- - - - - - - - -
#endif

Example of #if-#else-#endif Preprocessor Directive

EXAMPLE

#include<stdio.h>
#define MAX 45
void
main ()
{
#if MAX > 40
  printf ("Yes, MAX is greater then 40.");
#else
  printf ("No, MAX is not greater then 40.");
#endif
}

Output :

Yes, MAX is Greater then 40.

The #elif Preprocessor Directive

The #elif preprocessor directive is similar to if-else ladder statement. It is used for checking multiple conditions, if the first condition will not satisfy, compiler will jump to #else block and check other condition is true or not and so on.

Syntax of #if-#elif-#else-#endif Preprocessor Directive

#if(condition)
- - - - - - - - - -
- - - - - - - - - -
#elif(condition)
- - - - - - - - - -
- - - - - - - - - -
#elif(condition)
- - - - - - - - - -
- - - - - - - - - -
#else
- - - - - - - - - -
- - - - - - - - - -
#endif

Example of #if-#elif-#else-#endif Preprocessor Directive

EXAMPLE

#include<stdio.h>
#define MKS 65
void
main ()
{

#if MKS>=78
  printf ("\nGrade A");

#elif MKS>=50
  printf ("\nGrade B");

#elif MKS>=25
  printf ("\nGrade C");

#else
  printf ("\nGrade D");

#endif

}

Output :

Grade B

The #ifdef Preprocessor Directive

The #ifdef preprocessor directive is used to check whether the macro-name is previously defined or not, if defined then the statements given between #ifdef and #else will get execute.

Syntax of #ifdef preprocessor directive

#ifdef macro-name
- - - - - - - - - -
- - - - - - - - - -
#else
- - - - - - - - - -
- - - - - - - - - -
#endif

Example of #ifdef preprocessor directive

EXAMPLE

#include<stdio.h>
#define MKS 65
void
main ()
{

#ifdef MONTH
  printf ("\nMONTH is defined.");

#else
  printf ("\nMONTH is not defined.");

#endif

}

Output :

MONTH is not defined.

The #ifndef Preprocessor Directive

The #ifndef preprocessor directive is used to check whether the macro-name is previously defined or not, if not defined then the statements given between #ifndef and #else will get execute.

Syntax of #ifndef preprocessor directive

#ifndef macro-name
- - - - - - - - - -
- - - - - - - - - -
#else
- - - - - - - - - -
- - - - - - - - - -
#endif

Example of #ifndef preprocessor directive

EXAMPLE

#include<stdio.h>
#define MKS 65
void
main ()
{

#ifndef MAX
  printf ("\nMAX is not defined.");

#else
  printf ("\nMAX is defined.");

#endif

}

Output :

MAX is not defined.

Note: The #ifdef and #ifndef can not use the #elif preprocessor directive.

The #line Preprocessor Directive

The #line preprocessor directive is used to overwrite the default value of pre-defined macro-names __LINE__ and __FILE__. By defalut __LINE__ displays the current line number and __FILE__ displays the current filename.

Example of #line preprocessor directive

EXAMPLE 1

#include<stdio.h>
void
main ()
{
  printf ("\nLine number %d", __LINE__);
  printf ("\nFile name %s", __FILE__);
}

Output :

Line number : 4
File name : LineDemo.c

Note : We have saved the above code in LineDemo.c file.

EXAMPLE 2

#include<stdio.h>
#line 25 Demo.c
void
main ()
{
  printf ("\nLine number %d", __LINE__);
  printf ("\nFile name %s", __FILE__);
}

Output :

Line number : 25
File name : Demo.c

The above code is still in LineDemo.c file but we have overwritten the line number and the file name using #line directive.

The #error Preprocessor Directive

The #error preprocessor directive is used to force the compile to stop compiling the source code. In other words we can manually force the compiler to stop compiling and give fatal error.

Example of #error preprocessor directive

EXAMPLE 1

#include<stdio.h>
void
main ()
{
#ifndef MAX
  printf ("\nMAX is not defined.");	//Statement   1
#else
  printf ("\nMAX is defined.");	//Statement   2
#endif

  printf ("\nEnd of program.");	//Statement   3
}

Output :

MAX is not defined.
End of program.

EXAMPLE 2

#include<stdio.h>
void
main ()
{
#ifndef MAX
#error MAX is not defined.		//Statement   1
#else
  printf ("\nMAX is defined.");	//Statement   2
#endif

  printf ("\nEnd of program.");	//Statement   3
}

The only difference between example 1 and 2 is the statement 1. In example 1, statement 1 will display message and continue to compile the rest of the code. In example 2, statement 1 will force the compiler to give fatal error and stop compiling the rest of the code.

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.