Bird
Raised Fist0
Cprogramming~5 mins

Define macro - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a macro in C?
A macro in C is a way to define a piece of code or a value that the compiler replaces before actual compilation. It is created using the #define directive.
Click to reveal answer
beginner
How do you define a simple macro for a constant value?
You use #define followed by the macro name and the value. For example:
#define PI 3.14 defines PI as 3.14.
Click to reveal answer
beginner
What happens when you use a macro in your code?
The preprocessor replaces every occurrence of the macro name with its defined value or code before the compiler runs.
Click to reveal answer
intermediate
How do you define a macro that takes arguments?
You define it like a function but with #define. For example:
#define SQUARE(x) ((x) * (x)) defines a macro to square a number.
Click to reveal answer
intermediate
Why should you use parentheses in macro definitions with arguments?
Parentheses ensure the correct order of operations when the macro is expanded. Without them, expressions can give wrong results.
Click to reveal answer
What does the #define directive do in C?
AStarts a comment
BDeclares a variable
CIncludes a library
DDefines a macro that replaces text before compilation
How would you define a macro named MAX with value 100?
Aconst int MAX = 100;
Bint MAX = 100;
C#define MAX 100
D#macro MAX 100
What is the output of this code?
#define DOUBLE(x) (2 * (x))
int a = DOUBLE(3 + 1);
A8
B7
C10
D6
Why use parentheses around macro arguments in definitions?
ATo ensure correct calculation order
BTo make macros faster
CTo prevent syntax errors
DTo declare variables
Which of these is a function-like macro?
A#define PI 3.14
B#define SQUARE(x) ((x) * (x))
Cint square(int x) { return x * x; }
D#include <stdio.h>
Explain what a macro is in C and how it works.
Think about how the compiler sees your code after preprocessing.
You got /4 concepts.
    Describe how to safely define a macro that takes arguments and why parentheses are important.
    Consider what happens when you pass expressions as arguments.
    You got /4 concepts.

      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