Difficulty: Easy
Correct Answer: 2
Explanation:
Introduction / Context:
Macros in C and C++ provide a powerful but simple textual substitution mechanism through the preprocessor. When learning about macros, textbooks usually classify them into a small number of basic types. Being familiar with this classification helps you understand how different macro definitions behave and what kind of arguments they accept. This question asks for the number of commonly described macro types and implicitly expects you to know what they are.
Given Data / Assumptions:
Concept / Approach:
Object-like macros behave like simple symbolic names. They do not take arguments and are replaced by a constant or an expression. An example is #define MAX_VALUE 100. Function-like macros, on the other hand, take parameters and resemble function calls in their usage: #define SQR(x) ((x) * (x)). When the preprocessor encounters SQR(5), it expands it according to the replacement list. Although there are many ways to combine macros with conditional directives, concatenation, and stringisation, the core classification is into these two basic types. Therefore, the number of basic macro types commonly described is two.
Step-by-Step Solution:
Step 1: Recall the definition of an object-like macro as a simple name replacement without parameters.
Step 2: Recall the definition of a function-like macro as a macro that includes a parameter list in its definition and is invoked with arguments.
Step 3: Recognise that most educational resources group macros into these two categories.
Step 4: Count these categories: object-like and function-like, which yields a total of two basic types.
Step 5: Choose the option that corresponds to this count, which is 2.
Verification / Alternative check:
Looking at official documentation for compilers or the C standard, you will find that the syntax for macro definitions distinguishes between definitions with and without parameter lists. Practical examples of macros in real code also fall into these two groups: constant style macros and parameterised expression macros. While there are many macros in large codebases, they usually conform to one of these two base forms, reinforcing the idea that two is the correct number of primary macro types.
Why Other Options Are Wrong:
Option 1: Suggests there is only one macro type, which ignores the clear syntactic difference between macros with and without parameters.
Option 3: Implies a third standard macro category that is not generally recognised in the basic classification.
Option 4: Overstates the number of macro types and does not match standard teaching material.
Common Pitfalls:
Learners sometimes confuse macro types with other preprocessor features such as conditional compilation or include directives, leading them to think there are many more categories. Another pitfall is to misuse function-like macros for complex expressions without proper parentheses, which can cause subtle bugs. Knowing that there are two basic macro shapes helps keep reasoning about preprocessor behaviour clear while encouraging the use of safer alternatives such as constants and inline functions in modern C++.
Final Answer:
There are 2 basic types of macros commonly described in C and C++: object-like and function-like macros.
Discussion & Comments