c programming preprocessor directives
Preprocessor directives is a program that processes the code before compilation.
It represents by start with symbol [#] processor directives is a line of code which is terminated by a semicolon(;)
The source code written by programmer is stored in the file program.c . This file is then processed by preprocessor and and expanded source code file general named program.
Preprocessor program provide processor directive which tell the compiler to process the source code before compilation
* Types of preprocessor directives:-
There are main three preprocessor directives :-
1. File Inclusion.
2. Macro substitution.
3. compilation control.
3. compilation control.
1. File Inclusion:-
File inclusion means including or using a header file in a program by using include keyword with preprocessor directives
For e.g:- #include<header file>
* Header file or standard file:-
These file contain definition of predefined function like, printf( ); ,scanf(); ,etc.
* User defined function:-
When a program became very large, it's good practise to divide it into smallar files and include whenever
for example:- #include<filename>
2. Macro substitution:-
Macro substitution is the shortcut that is always or name which is replaced by a value & string.
Macro substitution creates by using a defined keyword which also begins with its symbol.
Macro substitution holds any kind of value we can not update the value of macro during the program.
For example:-#incluede<stdio.h>
#include<conio.h>
# define Line 5
void main()
{
int i ;
for(i=1;i<=line;i++)
{
printf("%d",i);
}
getch();
}
OUTPUT:-
1
2
3
4
5
To chech which variable may be update or not:-
#include<stdio.h>
#include<conio.h>
#define x 100
void main()
{
int z=10;
printf("%d",z);
printf("%d",x);
z=z+1; ---------------------------------[it is possible because it is local variable]
x=x+1;----------------------------------[it is not possible because it is macro]
getch();
3. Compilation Control:-
The conditional directives are a type of directives that helps to compile a specific portion of the program or to skip compilation of some specific part of the program based on some condition.This can be done with the help of two preprocessing commands ['ifdef' and 'endif']
Syntax
#ifdef macro_namestatement 1:
statement 2:
statement 3:
.
.
.
.
.
.
statement n;
#endif
Explaination of some preprocessor diectives :
* #undef
To undefine a macro means to cancel its definition. This is done with the #undef directives.
syntax: #undef token* #ifdef
The ifdef preprocessor directives checks if macro is defined by #define . If yes , it excutes the code.
syntax:
#ifdef MACRO
//code
#endif
* #ifndef
The #ifndef preprocessor directives checksif macro isn't defined by #define. If yes, it executes the code.
syntax:-
#ifndef MACRO
//code
#endif
* #if
The #if preprocessor directivs evalutes the expressiion or condition if condition of #if is true, it execute the code.
Syntax:
#if expression
//code
#endif
* #else
The #else preprocessor directive evalutes the expression or condition if condition is #if is false.
Syntax:
#if expression
// if code
#else
//else code
#endif
* #Error
The #error preprocessor directive indicates error. The compiler gives fatal error if #error directive is found.
Great
ReplyDeleteReally so
ReplyDeleteGood