C Preprocessors Macro Simple Parameterized By Asst. Prof. Sonali Gholve- Doifode
Preprocessor C Source code Preprocessor Compiler • Preprocessor is a collection of special statements, called directives, that are executed at the beginning of the compilation process. • # • Eg. #include => include the header file into the program. • #include<stdio.h>, #include<string.h>
Macro • What is macro? • Macro is a small part of a code that is given a name. Whenever the name is used, it is replaced by the contents of the macro. • you can define macro in C using #define preprocessor directive • Simple macro substitution • Parameterized macro/function like macro
Simple macro substitution • It is used to simple replacement of identifier by string. • Eg. To calculate area of circle using macro #include<stdio.h> #define PI 3.14 int main() { float rad, area; printf(“ Enter the radius”); scanf(“%f”,&rad); area=PI *rad*rad; printf(“Area of circle=%f”, area); return 0; }
Parameterized macro/Function like macro substitution • Macro that takes the parameter. • Eg. To calculate square of number using macro #include<stdio.h> #define square (x) ((x)*x) int main() { int p; p=square(5); printf(“n Square of 5 is=%d”, p); return 0; }

Macro and Preprocessor in c programming

  • 1.
  • 2.
    Preprocessor C Source code Preprocessor Compiler •Preprocessor is a collection of special statements, called directives, that are executed at the beginning of the compilation process. • # • Eg. #include => include the header file into the program. • #include<stdio.h>, #include<string.h>
  • 3.
    Macro • What ismacro? • Macro is a small part of a code that is given a name. Whenever the name is used, it is replaced by the contents of the macro. • you can define macro in C using #define preprocessor directive • Simple macro substitution • Parameterized macro/function like macro
  • 4.
    Simple macro substitution •It is used to simple replacement of identifier by string. • Eg. To calculate area of circle using macro #include<stdio.h> #define PI 3.14 int main() { float rad, area; printf(“ Enter the radius”); scanf(“%f”,&rad); area=PI *rad*rad; printf(“Area of circle=%f”, area); return 0; }
  • 5.
    Parameterized macro/Function like macrosubstitution • Macro that takes the parameter. • Eg. To calculate square of number using macro #include<stdio.h> #define square (x) ((x)*x) int main() { int p; p=square(5); printf(“n Square of 5 is=%d”, p); return 0; }