Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Friday, 11 March 2022

54.Preprocessor directives in C language, Rules in writing preprocessor statements ,Types of preprocessor statements, Macro substitution , Execution , Macro as a part of program, Macro with arguments, Method1

 

Preprocessor directives in C language:

Preprocessor is mechanism works in association with the C compiler.

The preprocessor executes only the preprocessor statements with in the program before compilation of the program.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Rules in writing preprocessor statements:

Every preprocessor statement must be started with character #

It must be started from the first column

There should be no space among # and remaining part.

It must not be terminated with a semicolon

Multiple statements cannot be written in single line

A preprocessor statement may be written any where within the program

 

Types of preprocessor statements:

C language supports 3 types of preprocessor statements that is

1. Macro substitution

2. Include statements

3. Conditional compilation statements.

4. Preprocessor operators.

 

Macro substitution:

Macro is a reusable code component. It has the name and value.

Here the name may be written in capitals to identify that it is a macro.

Here the value may be a numeric constant, a word, a statement or a part of program.

It is defined with #define statement

Macro is not a variable, it doesn't take any memory.

The value of a macro can't be changed by assigning a new value.

Macro is substituted along the program with its value before compilation of the program.

One macro can be reused in another macro.

Syntax:

 

#define <name>  <value>

 
 

 


Macro as a constant:

Criteria used in an application may change time to time; it may be difficult to modify the total application whenever criteria change

If a criterion is defined with a macro then it is enough to modify at a single place rather modifying the total application.

 

Example1:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Execution:

Enter the radios: 15

Area 706.500000

Circumference 94.200005

  

Example explained:

While executing the program PI is replaced with 3.14 by pre-processor in the source code.

If we want to change the value of PI as 3.142857 instead 3.14 then we no need to change the total program, it is enough to change the macro definition as

#define PI 3.142857

 

Example2:

#define D 3.75

#define C 6.80

#define I 12.50

#define A 0.50

#include<stdio.h>

void main()

{

int cmr,pmr,nu;

char type;

float bill;

clrscr();

printf("The type of connection [domestic/commercial/industrial/agriculture] d/c/i/a:");

scanf("%c",&type);

printf("Enter the current month meter reading:");

scanf("%d",&cmr);

printf("Enter the previous month meter reading:");

scanf("%d",&pmr);

nu=cmr-pmr;

if(type=='d')

   bill=nu*D;

else if(type=='c')

   bill=nu*C;

else if(type=='i')

   bill=nu*I;

else

   bill=nu*A;

printf("Number of units consumed %d",nu);

printf("\nTotal bill %f",bill);

getch();

}

 

Execution:

The type of connection [domestic/commercial/industrial/agriculture] d/c/i/a: c

Enter the current month meter reading: 500

Enter the previous month meter reading: 200

Number of units consumed: 300

Total bill 2040

 

Example explained:

If the tariff changes in future then the macros may be changed without changing the code of application.

 

Macros as words:

A word can be defined as macro.

It is mostly done to improve the readability and understandability of application.

 

Example:

#define out printf

#define in scanf

#include<stdio.h>

void main()

{

int x,y,z;

clrscr();

out("Enter two numbers:\n");

in("%d%d",&x,&y);

z=x+y;

out("Sum=%d",z);

getch();

}

 

Execution:

Enter two numbers:

6

9

Sum=15

 

Example explained:

The preprocessor replaces "printf" in place of "out" and "scanf" in place of "in" before compilation of program.

 

Macro as a part of program:

A statement or number of statements can be defined with a macro.

 

Example1:

#define B void main()

#define C }

#include<stdio.h>

B

{

clrscr();

printf("Hello world");

getch();

C

 

Output:

Hello world

 

Example explained:

Here B is replaced with void main() and C is replaced with } by the preprocessor before compilation.

 

Example2:

#define B void main()\

              {\

              clrscr();

#define C getch();\

              }

#include<stdio.h>

B

printf("Hello world");

C

Output:

Hello world

 

Example explained:

Here multiple statements are defining as macro.

\ is used to extend a line.

 

Macro with arguments:

A macro can be defined with parameters.

Unlike functions, arguments will not pass to the macro rather expression will be substituted.

It saves time comparatively with functions.

It takes no memory.

Simple functions can be replaced with macros to improve the performance and reduce the memory wastage.

But complex functions can't be replaced with macros.

 

Example1:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Output:

Enter two sides of rectangle:

4

5

Area 20

Perimeter 18

 

Example explained:

The function call area(l,b) is replaced with l*b

The function call peri(l,b) is replaced with 2*(l+b)

 

 

 

Example2:

#define PI 3.14

#define area(r) PI*r*r

#define cir(r) 2*PI*r

#include<stdio.h>

void main()

{

int rad;

clrscr();

printf("Enter the radios:");

scanf("%d",&rad);

printf("Area %f",area(rad));

printf("\nCircumference %f",cir(rad));

getch();

}

 

Execution:

Enter the radios: 5

Area 78.500000

Circumference 31.400000

Note:

  1. If the function is defined after the main() then it is mandatory to write the function declaration statement (prototype) before main()
  2. If the function is defined before the main() then it is not needed to write the prototype.

Example:

#include<stdio.h>

void main()

{

    clrscr();

    printf("Sum %f",sum(12.50,15.75));

    getch();

}

float sum(float x,float y)

{

    return x+y;

}

 

Output:

Error: Type mismatch in re declaration of 'sum'

 

Example explained:

It has no prototype.

It can be rectified in two ways that is either by writing the prototype or defining the function before main()

 

 

Method1:

#include<stdio.h>

float sum(float,float);  /* prototype */

void main()

{

    clrscr();

    printf("Sum %f",sum(12.50,15.75));

    getch();

}

float sum(float x,float y)

{

    return x+y;

}

 

Output:

Sum 28.250000

0 comments:

Post a Comment