Bird
Raised Fist0
Cprogramming~20 mins

Define macro - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Macro Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this macro usage?
Consider the following C code using a macro. What will be printed when this program runs?
C
#include <stdio.h>
#define SQUARE(x) x * x

int main() {
    int a = 3;
    printf("%d\n", SQUARE(a + 1));
    return 0;
}
A16
B7
CError: macro expansion invalid
D10
Attempts:
2 left
💡 Hint
Remember how macros expand without parentheses around parameters.
❓ Predict Output
intermediate
2:00remaining
What is the output of this corrected macro?
Now consider this corrected macro definition. What will be printed?
C
#include <stdio.h>
#define SQUARE(x) ((x) * (x))

int main() {
    int a = 3;
    printf("%d\n", SQUARE(a + 1));
    return 0;
}
AError: macro expansion invalid
B10
C16
D7
Attempts:
2 left
💡 Hint
Parentheses in macros help control the order of operations.
❓ Predict Output
advanced
2:00remaining
What is the output of this macro with side effects?
Look at this code using a macro that doubles a value. What will be printed?
C
#include <stdio.h>
#define DOUBLE(x) ((x) + (x))

int main() {
    int i = 2;
    printf("%d\n", DOUBLE(i++));
    return 0;
}
AUndefined behavior or unexpected result
B5
C4
D6
Attempts:
2 left
💡 Hint
Macros can cause expressions to be evaluated multiple times.
🧠 Conceptual
advanced
1:30remaining
What does this macro do?
Given the macro below, what is its purpose?
C
#define MAX(a,b) ((a) > (b) ? (a) : (b))
AReturns the sum of a and b
BReturns the smaller of a and b
CReturns true if a equals b
DReturns the larger of a and b
Attempts:
2 left
💡 Hint
Look at the conditional operator ? :
🔧 Debug
expert
2:30remaining
Why does this macro cause a compilation error?
Examine the macro and code below. Why does it cause a compilation error?
C
#define INCREMENT(x) x++

int main() {
    int a = 5;
    int b = INCREMENT(a) * 2;
    return 0;
}
ABecause macro expansion lacks parentheses causing wrong operator precedence
BBecause x++ cannot be used inside macros
CBecause the macro is missing a semicolon
DBecause multiplication operator * is invalid here
Attempts:
2 left
💡 Hint
Think about how the macro expands and operator precedence.

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