Q1 What is C Preprocessor? Types of C Preprocessors? Marco expansion, File Inclusion, Conditional Compilation, Miscellaneous Directives

What is C Preprocessor? Types of C Preprocessors? Marco expansion, Global vs UPPERCASE variable

Program: 145

What is C Preprocessor? Types of C Preprocessors? Marco expansion, File Inclusion, Conditional Compilation, Miscellaneous Directives

Definition:

It is a program that processes our source program before it is passed to the compiler. (C Preprocessor commands also known as directives)

C PREPROCESSOR

There are four types of directives exists in C preprocessor

  1. Macro expansion
  2. File Inclusion
  3. Conditional Compilation
  4. Miscellaneous directives

MACRO EXPANSION

#define PI 3.14
#define AND &&
#define OR ||

Macros with Arguments

#define AREA(x) (3.14 * x * x)

FILE INCLUSION

#include<stdio.h>
#include<math.h>

CONDITIONAL COMPILATION

ifdef

#include<stdio.h>
int main()
 {
     #ifdef INTEL
         code suitable for an Intel PC
     #else
         code suitable for a Motorola PC
     #endif
         code common to both the computer
 }

ifndef

#include<stdio.h>
#define INTEL
int main()
 {
     #ifndef INTEL
         code suitable for an Intel PC
     #else
         code suitable for a Motorola PC
     #endif
         code common to both the computer
 }

if and elif

include
 int main()
 {
     #if ADAPTER==VGA
         code for video graphics array
     #elif ADAPTER==SVGA
         code for super video graphics array
     #endif // ADAPTER
 }

MISCELLANEOUS DIRECTIVES

There are two more preprocessor directives available, through they are not very commonly used.

a. #undef
b. #pragma

#undef

It is used to undefine a macro that has been earlier #defined, the directive,

#undef PI

#pragma startup and #pragma exit

These directives allow us to specify functions that are called upon program startup (before main()) or program exit (just before the program terminates)

include
 void fun1();
 void fun2();
 pragma startup fun1
 pragma exit fun2
 int main()
 {
     printf("Inside main\n");
     return 0;
 }
 void fun1()
 {
     printf("Inside fun1\n");
 }
 void fun2()
 {
     printf("Inside fun2\n");
 }

Output

Inside fun1
Inside main
Inside fun2

#pragma warn

#pragma warn -rvl  /* return value */#pragma warn -par /* parameter not used */#pragma warn -rch /* unreachable code */

Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.
Related Post
Leave a Comment

This website uses cookies.