Output of the Program | Use Macros Carefully!

Predict the output of the below program

filter_none

edit
close

play_arrow

link
brightness_4
code

#define square(x) x*x
int main()
{
  int x;
  x = 36/square(6);
  printf("%d",x);
   
  getchar();
  return 0;
}

chevron_right


Output: 36

Explanation:
Preprocessor replaces square(6) by 6*6 and the expression becomes x = 36/6*6 and value of x is calculated as 36. If we want correct behavior from macro square(x), we should declare it as



#define square(x) ((x)*(x)) /* Note that the expression
(x*x) will also fail for square(6-2) */



My Personal Notes arrow_drop_up


Article Tags :

4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.