Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

clang

Preprocessors directives in C

Puppala Surendra Babu

Overview

Before we move on to the main topic we have to learn some basics regarding our concepts like preprocessor, and directives.

  • Directive: They mainly instruct our compiler on how it should process the input data. It can be done with the help of the preprocessors.

  • Preprocessor: It is a computer code that is made to run all our computers programs, application programs, and also the hardware.

What is a preprocessor directive?

These are some words that we should add at the start of our program as they make the main code text in our program executable.

They always begin with the character symbol #, and it is followed by some specific directive name. They are called by the compiler before the step of the compilation to make the process of our program.

Note: The preprocessor directives are executed before compilation.

Preprocessor directives in C Language

The C preprocessors are not part of our compiler, but they are some of the separate steps in the overall compilation process.

The commands that we use in the C preprocessors are called the C preprocessor directives.

In C language, we can define it as a macro processor to transform our code before its compilation. Some of the examples are:

  • #define
  • #if
  • #include
  • #error

It is also known as the macro preprocessor as they use macros. The block diagram shows the preprocessor in the C language:

Preprocessor directives in C
  1. #include: It includes a particular header from another file.
#include<stdio.h>
int main() 
{
  printf("Hello Educative.io");
  return 0;
}
  1. #define: It allows the definition of macros with source code.
#include <stdio.h>  
#define MAX(s,v) ((s)>(v)?(s):(v))  
int main() 
{  
   printf("Maximum of 4 and 6 is: %d\n", MAX(4,6));
   return 0;
}  
  1. #undef: It removes all definitions that are pre-specified.
#include<stdio.h>  
#define num 6  
int div = num / num;  
#undef num 
int main() 
{  
   printf("%d",div);  
   return 0;
}  
  1. #ifdef: It allows for a conditional compilation process.
  2. #else: It is quite opposite to #if directive.
  3. #endif: It closes the #if, #ifdef, or #ifndef directives directly.
#include <stdio.h>  
#define NOINPUT  
void main() 
{  
int s = 0;  
#ifdef NOINPUT  

s = 6;;  
#else  
printf("Enter value of s:");  
scanf("%d", &s);  

#endif         
printf("Value of s: %d\n", s);  
}  
  1. #if: It checks if the given condition is true.
  2. #else: It is totally opposite to if.
#include <stdio.h>   
#define NUMBER 0  
int main() 
{  
#if (NUMBER==0)  
printf("Value of Number is: %d",NUMBER);  
#else
printf("value is not defined");
#endif          
}  
  1. #error: It just throws some error.
  2. #indef: If macros are not defined, it returns true.
#include<stdio.h>  
#ifndef __MATH_H  
#error First include then compile  
#else  
void main(){  
    float s;  
    s = sqrt(6);  
    printf("%f",s);  
}  
#endif 
  1. #pragma: It provides more information (commands) to the compiler.

#include<stdio.h>    
#pragma startup func    
  
void main()
{  
printf("\nI am in main");  
}  
Flowchart of program execution

Macros

It is part of code that is given a name and is replaced by the value of a macro. It is defined by #define. There are two types of macros:

  • Object like macros
 #define PI 3.14
  • Function like macros
#define MAX(s,v) ((s)(v)?(s):(v))

Note: Here PI and MAX are the names of the macro.

RELATED TAGS

clang
RELATED COURSES

View all Courses

Keep Exploring