0
0
Cprogramming~5 mins

Define macro - Cheat Sheet & Quick Revision

Choose your learning style9 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: <br>#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:<br>#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?<br>#define DOUBLE(x) (2 * (x))<br>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.