Question

    What is the purpose of the #define directive in

    programming languages like C and C++?
    A It is used to declare a new variable and its data type in the program. Correct Answer Incorrect Answer
    B It is used to create a macro that defines a constant value or a code snippet that can be reused throughout the program. Correct Answer Incorrect Answer
    C It is used to import libraries and modules into the program. Correct Answer Incorrect Answer
    D It is used to initialize a new object or class instance with specific values. Correct Answer Incorrect Answer
    E It is used to specify the return type of a function and its parameters. Correct Answer Incorrect Answer

    Solution

    The #define directive in C and C++ is a preprocessor command used to define macros. Macros can be constants or code snippets that are substituted into the program wherever they are referenced. For example, #define PI 3.14 defines a constant named PI with the value 3.14 . Similarly, #define SQUARE(x) ((x) * (x)) defines a macro for computing the square of a number. This directive helps in making the code more readable, manageable, and easier to maintain by avoiding the repetition of constants and code fragments. It operates at the preprocessing stage before the actual compilation of the code begins.

    Practice Next