C Program To Find Area of Circle Using Macro Expansion


In this video tutorial lets learn about Macro Expansion and also lets write a simple program to find area of Circle using Macros.

Related Read:
Preprocessors In C Programming Language

Rules To Construct/Write Macro Definition

1. Space between # and define is optional.
2. There must be a space or tab between Macro Template and Macro Expansion.
3. #define MACRO_TEMPLATE MACRO_EXPANSION is called Macro Definition.
4. Macro definition must not end with semi-colon.
5. Using all capital letters for Macro template is convention and not a syntax of C. You can use any name for Macro template, except the C reserve words.

Video Tutorial: C Program To Find Area of Circle Using Macro Expansion



YouTube Link: https://www.youtube.com/watch?v=fMe5ieQY_Zk [Watch the Video In Full Screen.]

Source Code: C Program To Find Area of Circle Using Macro Expansion

  1. #include<stdio.h>  
  2.   
  3. #define PI 3.14  
  4.   
  5. int main()  
  6. {  
  7.     float r, area;  
  8.   
  9.     printf("Enter Radius of Circle\n");  
  10.     scanf("%f", &r);  
  11.   
  12.     area = PI * r * r;  
  13.   
  14.     printf("Area of Circle is %f\n", area);  
  15.   
  16.     return 0;  
  17. }  

Output 1:
Enter Radius of Circle
5
Area of Circle is 78.500000

Source Code: Writing printf statement in Macro expansion

  1. #include<stdio.h>  
  2.   
  3. #define DISPLAY printf("I'm in Love with iPhone UI Elements\n\n")  
  4.   
  5. int main()  
  6. {  
  7.     DISPLAY;  
  8.     return 0;  
  9. }  

Output 1:
I’m in Love with iPhone UI Elements

Note: Careful not to enter semicolon at the end of Macro definition. i.e., after the end of printf() statement.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *