Open In App

Output of the Program | Use Macros Carefully!

Improve
Improve
Like Article
Like
Save
Share
Report

Predict the output of the below program  

C++




#include <iostream>
using namespace std;
 
#define square(x) x*x
 
int main()
{
  int x;
  x = 36 / square(6);
   
  cout << x;
   
  return 0;
}
 
// This code is contributed by sarajadhav12052009


C




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


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) */


Last Updated : 21 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads