Preprocessor Directives in C Basic

A program goes from a few phases before getting executed. The program we compose is changed over into source code by utilizing a word processor which is put away with expansion .C. After that, it is changed over to extended source code by utilizing preprocessor which is put away with augmentation .I. Presently this extended source code is changed over to question code by utilizing compiler which is put away with expansion.OBJ. After that, it is changed over into executable code by utilizing Linker and put away with expansion.EXE.

Preprocessor Directives in C

Preprocessor order is a book substitution device that educates the compiler to pre-processor our program before its real arrangement. Every C preprocessor begins with # image. By and large, they are characterized toward the start of the program soon after the header documents. In any case, you can characterize them anyplace in the program.

Sorts of Preprocessor Directives in C

There are four sorts of preprocessor orders which are given beneath.

Macros

Document incorporation

Contingent assemblage

Random orders

Macros

Macros are commonly used to give a typical name to the qualities which are utilized ordinarily in a program. Or then again we can say that macros are utilized to characterize emblematic constants. Consider underneath program to get it.

/C program to calculate area of circle
 
#include <stdio.h>
 
#define PI 3.14  //macro
 
int main()
{
 float r, a;
 printf("Enter radius of the circlen");
 scanf("%f",&r);
 a=PI*r*r;
 printf("Area of the circle is %f",a);
 
 return 0;
}

Output

Enter radius of the circle
4
Area of the circle is 50.240002

Focuses to recollect

Macros consistently start with #define. In our program #define PI 3.14 is the definition of the macro. Any place PI is experienced in the program it is supplanted with 3.14.

In the gathering of the program, every one of the definitions of the macro supplanted with the qualities which are utilized with #define. It is done in the process when source code is changed over into extended source code.

Try not to include semicolon toward the end.

For the most part, macros are written in a single line. In the event that you need to compose it in numerous lines, at that point you need to utilize full-scale continuation administrator for example. A model for this is given beneath.

Examples

#define OR ||
#define CHECK (a>10 && a<20)
#define MSG
printf(“It is working fine”)

Macros with contentions

Macros with contentions are like capacities with contentions. Consider the underneath program to comprehend its working.

//C program to calculate perimeter of circle
 
#include<stdio.h>
 
#define PERI(x) (2*3.14*x)  //macro with argument
 
int main()
{
 float r,per;
 printf("Enter radius of the circlen");
 scanf("%f",&r);
 per=PERI(r);
 printf("Perimeter of the circle is %f",per);
 
  return 0;
}

Output

Enter radius of the circle
5
Perimeter of the circle is 31.400000

Focuses to recollect

In the above program, I have utilized macros with contention with the announcement #define PERI(x) (23.14x)

Be cautious while characterizing macros with contentions. In above model, there must be no space among PERI and (x). The body of the full scale ought to be set in enclosures.

At whatever point the full-scale calling is done in our program, its calling explanation in supplanted by its body.

They are quick when contrasted with capacities since control doesn’t go to full-scale definition, the entire body comes at where the call is finished.

File Inclusion

As its name proposes this preprocessor order is utilized to incorporate all the substance of document to be incorporated into the source code. Till now we have utilized preprocessor orders like #include which incorporates every one of the substance from the header document stdio.h. These are predefined header documents, you can likewise make your own and afterwards incorporate it in your program.

Examples

#include<math.h>
#include<stdlib.h>

Why it is utilized?

It’s positively a decent question. It’s anything but a decent programming practice to compose full program into a solitary document.

While composing a mind-boggling program it is prescribed to break the program into records and incorporate them in the first place.

In some cases the macros of the program are likewise very huge which must be put away in certain documents. So it is fundamentally utilized for better administration of the C program.

DURGESH

I Love python, so I like machine learning a Lot and on the other hand, I like building apps and fun games I post blogs on my website for Tech enthusiast to learn and Share Information With The World.

Leave a Reply

Your email address will not be published. Required fields are marked *

2 × 3 =

Recent Content