0
0
Cprogramming~15 mins

Macro with arguments - Mini Project: Build & Apply

Choose your learning style9 modes available
Macro with arguments
📖 Scenario: You are writing a small C program to calculate the area of rectangles quickly using macros.
🎯 Goal: Build a macro that takes two arguments (width and height) and calculates the area of a rectangle.
📋 What You'll Learn
Create a macro named AREA that takes two arguments: width and height.
Use the macro to calculate the area of rectangles with given dimensions.
Print the calculated area.
💡 Why This Matters
🌍 Real World
Macros with arguments are used in C programming to write reusable code snippets that can perform calculations or operations quickly without function call overhead.
💼 Career
Understanding macros helps in embedded systems programming, performance-critical applications, and working with legacy C codebases.
Progress0 / 4 steps
1
Create width and height variables
Create two integer variables called width and height and set them to 5 and 10 respectively.
C
Need a hint?

Use int width = 5; and int height = 10; inside main().

2
Define the AREA macro
Define a macro named AREA that takes two arguments width and height and returns their product. Place the macro definition above main().
C
Need a hint?

Use #define AREA(width, height) ((width) * (height)) to create the macro.

3
Calculate area using the AREA macro
Create an integer variable called area and set it to the result of AREA(width, height).
C
Need a hint?

Use int area = AREA(width, height); inside main().

4
Print the calculated area
Use printf to print the text "Area: " followed by the value of area and a newline.
C
Need a hint?

Use printf("Area: %d\n", area); to display the result.