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
Page Contents
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 https://www.youtube.com/watch?v=fMe5ieQY_Zk]
Source Code: C Program To Find Area of Circle Using Macro Expansion
#include<stdio.h> #define PI 3.14 int main() { float r, area; printf("Enter Radius of Circle\n"); scanf("%f", &r); area = PI * r * r; printf("Area of Circle is %f\n", area); return 0; }
Output 1:
Enter Radius of Circle
5
Area of Circle is 78.500000
Source Code: Writing printf statement in Macro expansion
#include<stdio.h> #define DISPLAY printf("I'm in Love with iPhone UI Elements\n\n") int main() { DISPLAY; return 0; }
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