What is the output of the following C program? #include <stdio.h> #define…

2024

What is the output of the following C program?

#include <stdio.h>

#define prod(a,b) a*b

int main()
{
    int x=3,y=4;
    printf("%d",prod(x+2,y-1));
    return 0;
}

Answer: A. 10ConceptA function-like C preprocessor macro performs textual substitution before compilation; it does not automatically add parentheses around its parameters…

  1. A.

    10

  2. B.

    20

  3. C.

    15

  4. D.

    0

Attempted by 242 students.

Show answer & explanation

Correct answer: A

Concept

A function-like C preprocessor macro performs textual substitution before compilation; it does not automatically add parentheses around its parameters or replacement body.

After expansion, the resulting C expression follows normal operator-precedence rules, where multiplication precedes addition and subtraction.

Application

  1. Substitute the arguments into a*b: prod(x+2,y-1) expands textually to x+2*y-1.

  2. Substitute x = 3 and y = 4 to obtain 3+2*4-1.

  3. Apply multiplication first: 2*4 = 8.

  4. Evaluate left to right at the addition/subtraction level: 3+8-1 = 10.

Cross-check

If the macro had been defined safely as ((a)*(b)), the call would produce (3+2)*(4-1)=15. That different result confirms why the missing parentheses change the output.

Result

The program prints 10.

Explore the full course: Accenture Preparation

Loading lesson…