Difficulty: Easy
Correct Answer: Incorrect
Explanation:
Introduction / Context:
This question targets the important distinction between preprocessor macros and real functions. While both may look similar at the call site, their semantics, type checking, evaluation rules, and debugging characteristics differ significantly.
Given Data / Assumptions:
Concept / Approach:
Macros do not have types, scope like functions, or parameter evaluation rules: their arguments may be evaluated multiple times or not at all depending on the macro body. Functions evaluate arguments before the call (with language-defined order constraints) and pass values according to the platform ABI. Tools like inline functions provide function-like efficiency while preserving type safety—still not the same as macros.
Step-by-Step Solution (differences):
Type checking: Functions are type-checked; macro usage is not, beyond textual substitution.Evaluation count: Macros can evaluate arguments multiple times, causing side effects to repeat; functions evaluate each argument exactly once.Debugging: Macros disappear after preprocessing; stepping through them is hard. Functions remain addressable and traceable.Scoping and linkage: Macros obey preprocessor scope; functions obey language scope and linkage rules.
Verification / Alternative check:
Compare a macro SQR(x) xx with a function sqr(int x){return xx;} Calling SQR(i++) differs from sqr(i++) due to repeated evaluation in the macro.
Why Other Options Are Wrong:
“Correct” ignores fundamental differences; tying identity to optimization level or inline functions is inaccurate; claiming it is true in C but not C++ is also wrong—both languages use a similar preprocessing model.
Common Pitfalls:
Using macros where functions or inline functions are safer; forgetting parentheses in macro definitions; assuming macros incur call overhead (they do not, but that is not equivalence).
Final Answer:
Incorrect
Discussion & Comments