-3
    #include <stdio.h>    
    #define SQRT(X)X*X
    int main(){
     int x=16/SQRT(4);
     printf("%d",x);
    
     return 0;
    }

this output is 16 why? firstly i defined macro and then try do calculation

Damien
  • 4,809
  • 4
  • 15
  • 20

1 Answers1

4

Currently int x=16/SQRT(4); will expand to int x=16/4*4; which is clearly 16. Use brackets to ensure the macro expansion is as intended:

#define SQRT(X) ((X)*(X))

Aside: SQRT is an odd name for the macro as that usually means "square root". SQR or SQUARE would be a more appropriate name.

kaylum
  • 13,833
  • 2
  • 22
  • 31