Bird
Raised Fist0
Cprogramming~15 mins

Define macro - Mini Project: Build & Apply

Choose your learning style10 modes available
Using #define Macro in C
📖 Scenario: You are writing a simple C program that needs to use a constant value multiple times. Instead of typing the number everywhere, you want to use a macro to make your code easier to read and change.
🎯 Goal: Learn how to create and use a #define macro in C to represent a constant value.
📋 What You'll Learn
Create a macro named PI with the value 3.14
Create a variable radius with the value 5
Calculate the area of a circle using the formula PI * radius * radius
Print the calculated area
💡 Why This Matters
🌍 Real World
Macros help programmers write cleaner code by replacing repeated values with easy names. This makes programs easier to read and update.
💼 Career
Understanding macros is important for C programming jobs, especially in embedded systems, game development, and performance-critical applications.
Progress0 / 4 steps
1
Create a macro for PI
Write a #define macro named PI with the value 3.14 at the top of your program.
C
Hint

Use #define PI 3.14 before int main().

2
Create a variable for radius
Inside the main function, create a variable named radius and set it to 5.
C
Hint

Write int radius = 5; inside main.

3
Calculate the area using the macro
Create a variable named area inside main and set it to PI * radius * radius.
C
Hint

Use float area = PI * radius * radius; to calculate the area.

4
Print the area
Write a printf statement to display the text "Area: " followed by the value of area with two decimal places.
C
Hint

Use printf("Area: %.2f\n", area); to print the area with two decimals.

Practice

(1/5)
1. What does the #define directive do in C?
easy
A. It creates a macro that replaces text before compilation.
B. It declares a variable with a fixed value.
C. It defines a function that runs at runtime.
D. It allocates memory for a constant.

Solution

  1. Step 1: Understand the purpose of #define

    The #define directive tells the compiler to replace all occurrences of a name with a given value or code snippet before the program runs.
  2. Step 2: Differentiate from variables and functions

    Unlike variables or functions, macros do not allocate memory or run at runtime; they are simple text replacements done before compilation.
  3. Final Answer:

    It creates a macro that replaces text before compilation. -> Option A
  4. Quick Check:

    #define = text replacement [OK]
Hint: Macros replace text before running code [OK]
Common Mistakes:
  • Thinking macros allocate memory
  • Confusing macros with functions
  • Assuming macros run at runtime
2. Which of the following is the correct syntax to define a macro named PI with value 3.14?
easy
A. #define PI 3.14
B. #define PI = 3.14
C. define PI 3.14
D. #define(PI, 3.14)

Solution

  1. Step 1: Recall correct #define syntax

    The correct syntax is #define NAME value without an equal sign or parentheses.
  2. Step 2: Check each option

    #define PI = 3.14 uses an equal sign which is incorrect. define PI 3.14 misses the #. #define(PI, 3.14) uses parentheses incorrectly. Only #define PI 3.14 matches the correct syntax.
  3. Final Answer:

    #define PI 3.14 -> Option A
  4. Quick Check:

    Correct macro syntax = #define NAME value [OK]
Hint: No equal sign in macro definitions [OK]
Common Mistakes:
  • Adding '=' sign after macro name
  • Omitting '#' in #define
  • Using parentheses like a function
3. What is the output of this C code?
#include <stdio.h>
#define SQUARE(x) ((x) * (x))

int main() {
    int a = 3;
    printf("%d", SQUARE(a + 1));
    return 0;
}
medium
A. Error
B. 7
C. 10
D. 16

Solution

  1. Step 1: Expand the macro with argument

    The macro call SQUARE(a + 1) expands to ((a + 1) * (a + 1)).
  2. Step 2: Calculate the expression

    Substitute a = 3: ((3 + 1) * (3 + 1)) = (4 * 4) = 16.
  3. Final Answer:

    16 -> Option D
  4. Quick Check:

    Macro expansion and calculation = 16 [OK]
Hint: Remember to expand macros fully before calculating [OK]
Common Mistakes:
  • Not using parentheses in macro causing wrong order
  • Calculating as 3 + 1 * 3 + 1 = 7
  • Assuming macro is a function call
4. Identify the error in this macro definition:
#define MAX(a, b) a > b ? a : b
medium
A. Macro names cannot have parameters
B. Incorrect use of ? operator
C. Missing parentheses around the entire macro expression
D. Missing semicolon at the end

Solution

  1. Step 1: Analyze macro expression grouping

    The macro expands to a > b ? a : b without parentheses, which can cause wrong evaluation when used in expressions.
  2. Step 2: Understand importance of parentheses

    Wrapping the entire macro in parentheses like ((a) > (b) ? (a) : (b)) ensures correct order and prevents bugs.
  3. Final Answer:

    Missing parentheses around the entire macro expression -> Option C
  4. Quick Check:

    Always parenthesize macro expressions [OK]
Hint: Wrap macro expressions in parentheses to avoid errors [OK]
Common Mistakes:
  • Not using parentheses around macro body
  • Assuming macros behave like functions
  • Adding semicolon inside macro definition
5. You want to create a macro MIN that returns the smaller of two values x and y. Which of the following is the best macro definition to avoid common bugs?
hard
A. #define MIN(x, y) ((x < y) ? x : y)
B. #define MIN(x, y) ((x) < (y) ? (x) : (y))
C. #define MIN(x, y) (x < y ? x : y)
D. #define MIN(x, y) x < y ? x : y

Solution

  1. Step 1: Check for parentheses around parameters

    #define MIN(x, y) ((x) < (y) ? (x) : (y)) wraps each parameter in parentheses to avoid issues when arguments are expressions.
  2. Step 2: Check for parentheses around entire expression

    #define MIN(x, y) ((x) < (y) ? (x) : (y)) also wraps the whole expression in parentheses to ensure correct evaluation in larger expressions.
  3. Step 3: Compare with other options

    Options A, B, and C miss some parentheses, risking wrong results or precedence bugs.
  4. Final Answer:

    #define MIN(x, y) ((x) < (y) ? (x) : (y)) -> Option B
  5. Quick Check:

    Parenthesize parameters and expression in macros [OK]
Hint: Always parenthesize macro parameters and full expression [OK]
Common Mistakes:
  • Missing parentheses around parameters
  • Missing parentheses around entire macro
  • Assuming macros behave like functions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard