15

Preprocessor

Embed Size (px)

Citation preview

Page 1: Preprocessor
Page 2: Preprocessor

PRE-PROCESSOR DIRECTIVES IN C

BY

JOHN JOSE

Page 3: Preprocessor

INTRODUCTION

• One of the unique feature of C language is

preprocessor.

• The pre processor is a program that processes the

source code before it passes through compiler

• Preprocessor directives are divided into three

categories,

1.Macro substitution directives

2.file inclusion directives

3.compiler control directives

Page 4: Preprocessor

Macro Substitution:

• Macro substitution is a process where an identifier in a program replaced by predefined string composed of one or more tokens

• It is achieved by #define directives

Page 5: Preprocessor

Examples

• #define PI 3.142

• #define TRUE 1

• #define AND &&

• #define LESSTHAN <

• #define MESSAGE "welcome to C"

Page 6: Preprocessor

Example(program by programmer)

#include<stdio.h>

#define LESSTHAN <

int main()

{

int a = 30;

if(a LESSTHAN 40)

printf("a is Smaller");

return(0);

}

Page 7: Preprocessor

Program is processed by pre-processor

int main()

{

int a = 30;

if(a < 40)

printf("a is Smaller");

return(0); }

Program is processed by compiler

a is Smaller

Page 8: Preprocessor

FILE INCLUSSION

• An external file containing functions or macro definition can be included as a part of program so that we need not rewrite those function or macro definition.

• This is achieved by #include directives

Page 9: Preprocessor

Example

• Step1 : Type this Code

• In this Code write only function definition as you

write in General C Program

int add(int a,int b){return(a+b);}

Page 10: Preprocessor

Step 2

• Save Above Code with [.h ] Extension .

• Let name of our header file be myhead [ myhead.h ]

• Compile Code if required.

Page 11: Preprocessor

Step 3 : Write Main Program

#include<stdio.h>#include"myhead.h"void main(){int number1=10,number2=10,number3;number3 = add(number1,number2);printf("Addition of Two numbers : %d",number3);}

Page 12: Preprocessor

• Include Our New Header File .

• Instead of writing < myhead.h> use this terminology “myhead.h”

• All the Functions defined in the myhead.h header file are now ready for use .

• Directly call function add(); [ Provide proper parameter and take care of return type ]

Note

While running your program precaution to be taken :

Both files [ myhead.h and sample.c ] should be in same folder.

Page 13: Preprocessor

ADDITIONAL PREPROCESSOR DIRECTIVES

• #ELIF DIRECTIVE: #ELIF Enable us to establish an if else sequence for testing multiple conditions.

• #PRAGMA DIRECTIVES: #PRAGMA Is an implementation oriented directive that allow us to specify various instruction to be given to compiler

Syntax: #pragma name

• #ERROR : #ERROR Directive is used to produce diagonastic messages during debugging

• Syntax :#ERROR error message.

Page 14: Preprocessor

PREPROCESSOR OPERATORS

• There are two preprocessor operators namely,

• 1. Stringizing operator (#): it is used in definition of macro functions. This operators allows a formal argument within macro definition to be converted to string

• Syntax: # define sum(xy)

printf(#xy”=%f/n”,xy);

Page 15: Preprocessor

2.TOKEN PASTING OPERATORS

The token pasting operator enables us to combine two within a macro definition to form a single token

Syntax : #define combine(string1,string2)s1##s2