Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the output of the following C program?

#include <stdio.h>
#define SQR(x) (x*x)
int main()
{
    int a;
    int b=4;
    a=SQR(b+2);
    printf("%d\n",a);
    return 0;
}

 

  • A.14
  • B.36
  • C.18
  • D.20

Answer: A

Correct answer: 14

With b equal to 4, the unparenthesised macro expands to

a = b+2*b+2;

and multiplication binds tighter than addition: \(4 + 8 + 2 = 14\). The intended \(6^2 = 36\) requires the parameters to be parenthesised: #define SQR(x) ((x)*(x)).

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.