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

Discussion

#include <stdio.h>
#define square(x) x*x
int main()
{
  int x;
  x = 36/square(6);
  printf("%d", x);
  return 0;
}

 

  • A.1
  • B.36
  • C.Compiler Error

Answer: B

Correct answer: 36

The macro has no parentheses, so square(6) expands to the bare text 6*6 and the statement becomes

x = 36/6*6;

/ and * have equal precedence and associate left to right, so this is \((36/6) \times 6 = 6 \times 6 = 36\), not 1. Writing the macro as #define square(x) ((x)*(x)) is what prevents this.

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

Post your comment

Please log in or register to post a comment.