‘C’ pre-processor

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.
  1. File inclusion.
  2. Macro substitution.
  3. 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”

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.

Program name: calc.c:

  1. int square(int x)

    {

              return x*x;

    }

    int cube(int x)

    {

              return x*x*x;

    }

Another program created for file inclusion:

  1. #include<stdio.h>

    #include "calc.c"

    void main()

    {

    int val;

    clrscr();

    printf("Enter a value: ");

    scanf("%d",&val);

    printf("Square root value is :%d",square(val));

    printf("\nCube value is :%d",cube(val));

    getch();

    }

Output:

Where,

“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

Example program:

#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();
}

Output:
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();
}

Output:

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me