Difficulty: Easy
Correct Answer: #define
Explanation:
Introduction / Context:
Macros are a feature of the C and C++ preprocessor that allow developers to define symbolic names or parameterised snippets of code that are expanded before compilation. They are widely used for constants, conditional compilation, and simple code generation. To define a macro, you must use a specific preprocessor directive. This question checks whether you know the exact keyword and syntax used for macro definition.
Given Data / Assumptions:
Concept / Approach:
In C and C++, preprocessor directives begin with a hash symbol at the start of the line. The directive used to define macros is #define. It is followed by the macro name and optionally by a replacement list or parameter list. For example, #define PI 3.14159 defines a constant-like macro, and #define SQR(x) ((x) * (x)) defines a function-like macro. The word define by itself is not recognised as a directive unless it is preceded by the hash symbol, and macro is not a keyword in the language.
Step-by-Step Solution:
Step 1: Recall that all preprocessor directives start with the hash character at the beginning of a line.
Step 2: Identify the directive used for macro definitions, which appears in examples as #define MACRO_NAME value.
Step 3: Recognise that the hash symbol is part of the directive keyword and cannot be omitted.
Step 4: Compare the options and see that #define matches the known directive exactly, including the hash symbol.
Step 5: Confirm that neither macro nor define alone are valid directives in C or C++ source code.
Verification / Alternative check:
If you write a small program and use define without the hash symbol, the compiler will treat it as an identifier, not as a directive, leading to compilation errors. However, when you use #define at the start of a line, the preprocessor recognises it and replaces occurrences of the macro name according to the definition. This behaviour can be tested easily and matches the explanation that #define is the correct keyword for macro definition.
Why Other Options Are Wrong:
Option macro: This word looks descriptive but is not a reserved keyword and has no special meaning to the preprocessor.
Option define: Without the hash symbol, define is just an identifier and not a preprocessor directive.
Option none of these: This is incorrect because #define is a standard and widely used directive in C and C++.
Common Pitfalls:
A common mistake is to forget the hash symbol, especially when first learning preprocessor directives. Another pitfall is to rely heavily on macros for tasks better handled by const variables or inline functions in C++, which can lead to harder to debug code. Nevertheless, knowing the basic syntax of #define is important for understanding legacy code and conditional compilation patterns.
Final Answer:
The keyword used in C and C++ to define macros is #define.
Discussion & Comments