Code:
#include<stdio.h>
#define SQR(x) x * x
int main()
{
printf("%d", 225/SQR(15));
}
|
Invasive contributor
|
![]() |
| 30Mar2010,21:42 | #1 |
|
Code:
#include<stdio.h>
#define SQR(x) x * x
int main()
{
printf("%d", 225/SQR(15));
}
|
|
Go4Expert Founder
|
![]() |
| 31Mar2010,07:32 | #2 |
|
Precedence is not working as you expect with the MACROS.
Try Code:
printf("%d", 225/(SQR(15)));
|
|
Go4Expert Member
|
|
| 31Mar2010,09:27 | #3 |
|
The macro is substituted like the following ,
Code:
#include<stdio.h>
#define SQR(x) x * x
int main()
{
printf("%d", 225/15*15);
}
|
|
Mentor
|
![]() |
| 31Mar2010,12:10 | #4 |
|
Better to use parentheses in the macro so that the macro can be used "normally". You wouldn't normally have to bracket a subexpression so requiring the programmer to remember for SQR that they have to do
Code:
225/(SQR(15)) So define the macro as Code:
#define SQR(x) ( (x) * (x) ) Code:
225/SQR(15) 225/SQR(7+8) The brackets around the individual x's mean you can put subexpressions into SQR. Without them, the second would be substituted as: Code:
225/(7+8*7+8) Code:
int i=15; int j=225/SQR(i++); Code:
#define SQR(x) (pow((x),2))
Scripting
like this
|