The
‘C’ pre-processor:
- These are placed before the main() function in the source program.
- The pre-processor operates under the following pre-processor directives.
- File inclusion.
- Macro substitution.
- Conditional inclusion.
Rules
for defining pre-processor:
- Every pre-processor must start with the # symbol.
- The pre-processor is always placed before the main() function.
- The pre-processor cannot have termination with a semicolon.
- There is no assignment operator in the #define statement.
- The conditional macro must be terminated (#ifdef,#endif).
File
inclusion:
This is used to include an external file, which
contains functions or some other macro definitions to our source program, so we
need not rewrite that functions and macros in our source program.
Syntax:
# include <file_name> and
# include “file_name”
# include <file_name> and
# include “file_name”
Where,
<file_name> is used searched only in standard directories.
“file_name” is used to search the file in your
current directory and then in standard directories.
“stdio.h” is the file, that contains standard input
and output function ‘C’ standard
directory.
“example.c” is the program written by the user, it
frequently occurs in the main source program.
Macro
substitutions:
- This is used to define symbolic constants in the source program.
- The identifier or string or integer defined is replaced by macro substitution.
- This statement is also placed before the main() function in the source program.
Syntax:
i). Simple macro:
Syntax:
# define identifier string/integer
Example:
#define A 20
ii). Argumented macros:
syntax:
#define identifier (v1 v2 v5...) string/integer
i). Simple macro:
Syntax:
# define identifier string/integer
Example:
#define A 20
ii). Argumented macros:
syntax:
#define identifier (v1 v2 v5...) string/integer
Example program:
Output:
#define sqr(n) n*n
#define cube(n) n*n*n
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=3,s,c;
clrscr();
s=sqr(a);
c=cube(b);
printf("\n Squre
value of 5 is %d\n",s);
printf("\n Cube
value of 3 is %d",c);
getch();
}
#define sqr(n) n*n
#define cube(n) n*n*n
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=3,s,c;
clrscr();
s=sqr(a);
c=cube(b);
printf("\n Squre
value of 5 is %d\n",s);
printf("\n Cube
value of 3 is %d",c);
getch();
}
Conditional inclusion:
These are used to control the pre-processer with
conditional statements.
Example program:
#define USD 1
#define UKP 1
#include<stdio.h>
#include<conio.h>
#ifdef USD
#define currency_rate 46
#endif
#ifdef UKP
#define currency_rate 100
#endif
void main()
{
int rs;
clrscr();
rs=10 * currency_rate;
printf("\n The
value of rs is: %d\n",rs);
getch();
}
#define USD 1
#define UKP 1
#include<stdio.h>
#include<conio.h>
#ifdef USD
#define currency_rate 46
#endif
#ifdef UKP
#define currency_rate 100
#endif
void main()
{
int rs;
clrscr();
rs=10 * currency_rate;
printf("\n The
value of rs is: %d\n",rs);
getch();
}
Output:
No comments:
Post a Comment