View
113
Download
2
Tags:
Embed Size (px)
Citation preview
PRE-PROCESSOR DIRECTIVES IN C
BY
JOHN JOSE
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
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
Examples
• #define PI 3.142
• #define TRUE 1
• #define AND &&
• #define LESSTHAN <
• #define MESSAGE "welcome to C"
Example(program by programmer)
#include<stdio.h>
#define LESSTHAN <
int main()
{
int a = 30;
if(a LESSTHAN 40)
printf("a is Smaller");
return(0);
}
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
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
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);}
Step 2
• Save Above Code with [.h ] Extension .
• Let name of our header file be myhead [ myhead.h ]
• Compile Code if required.
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);}
• 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.
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.
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);
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