0% found this document useful (0 votes)
8 views2 pages

Preprocessors

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Preprocessors

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Preprocessors 2.

Source File Inclusion Directive: (#include)


 The preprocessor is a translator that converts a program It tells the preprocessor to replace the directive with the
written in one high-level language into an equivalent content of the file specified in the directive. It can be written as,
program written in another language. a. #include<filename>
 Compiler converts a program written in a high-level language b. #include “filename”
into a n equivalent program either in an assembly-level c. #include token-sequence
language.
 Assembler converts assembly language program into Example:
machine code. // File name areaofcir.c
 Interpreter is a translator that converts the statements written #include<stdio.h>
in a high-level language program into machine language void disp()
code. {
float r,pi;
The various preprocessor directives available in C language are as pi=3.14;
follows: printf("Enter r:");
1. Macro replacement directive (#define, #undef) scanf("%f",&r);
2. Source file inclusion directive (#include) printf("Area of circle : %f",pi*r*r);
3. Line directive (#line) }
4. Error directive (#error)
5. Pragma directive (#pragma) //Filename areadisp.c Output:
6. Conditional compilation directives (#if, #else, #elif, #include "areaofcir.c"
Enter r:5
#endif, #ifdef, #ifndef) void main()
7. Null directive (#new-line) { Area of circle : 78.500000
1. Macro Replacement Directives: disp();
Macro is a token that can be replaced by the user-defined }
sequence of characters. There are two types of macros:
1. Macro without arguments, also called object-like 3. Line directive: (#line)
macros. It is used to reset the line number and the file name as
2. Macro with arguments, also called function-like macros. reported by __LINE__ and __FILE__ macros. It defined as,
#line constant
Object-like Macros: (#define) #line constant “filename”
It is also known as symbolic constant. It is defined as,
#define macro-name replacement-list Example:
Example: #include<stdio.h>
#include<stdio.h> void main()
#define pi 3.14 {
void main() printf("Line number is : %d”, __LINE__);
{ #line 200
int r; printf("Line number is : %d”, __LINE__);
printf("Enter r:"); }
scanf("%d",&r); Output:
printf("Area of circle : %f",pi*r*r); Line number is : 4
} Line number is : 200
Function – like Macro : (#define) 4. Error directive: (#error)
A macro with arguments is called a function-like macro. It causes the preprocessor to generate the customized
Its usage is syntactically similar to a function call. It can be diagnostic messages and causes the compilation to fail. It defined
defined as, as,
#define macro-name(parameter-list) replacement-list #error
\ Example: #error token-sequence
#include<stdio.h>
#define square(x) x*x Example:
void main() #include<stdio.h>
{ #error User defined error messages
int x; void main()
printf("Enter a number:"); {
scanf("%d",&x); printf("Compilation Failed:");
printf("Square : %d",square(x)); }
}
Output: Output:
Enter a number:5 fatal error C1189: #error : User defined error messages
Square : 25
5. Pragma directive: (#pragma) S. Conditional
It is used to specify diverse options to the compiler. The Syntax
No compilation directive
options are specific for the compiler and the platform used. It is
#if constant-expresion
written as,
1. #if - #endif Statements
#pragma token-sequence
#endif
#pragma option
#if constant-expression
#pragma warn
Statements
#pragma option
2. #if - #else - #endif #else
Some pragma options available with Turbo C 3.0,
Statements
SNo Options Role #endif
#if constant – expression
1. -C Allows nested comments
Statements
2. -C- Doesn’t allows nested comments in fav 3. #if - #elif - #endif #elif constant – expression
1. Statements
-r Enables the use of register variables #endif
2. #ifdef identifier
-r- Suppresses the use of register variables 4. #ifdef - #endif Statement
#endif
#include<stdio.h>
#pragma option -C Example:
void main() #include<stdio.h>
{ #define pi
/* Program with #pragma void main()
/* To display Welcome msg*/ {
*/ #ifdef pi
printf("Welcome"); printf("PI - defined");
} #else
Output: printf("PI - undefined");
Welcome #endif
}
#pragma warn
Used to turn on, off or toggle the state of warnings. Output:
S. Warning
Warning PI - defined
No Code
1. dup Redefinition of macro is not the same.
2. voi Void functions may not return a value 7. Null Directive
3. par Parameter is never used It is defined as,
4. rch Unreachable code #new-line
The null directive has no effect.
5. pia Incorrect Assignment

Example
#include<stdio.h>
#pragma warn -pia
void main()
{
int a;
a=11;
if(a=0)
printf("Null");
else Output: Not null
printf("Not null");
}

6. Conditional Compilation Directives:


Program is compiled only if a certain condition comes
out to be true. The available conditional compilation directives
are as follows,
#if, #ifdef, #ifndef, #else, #elif, #endif

You might also like