![]() |
Why this program outputs 225 istead of 1 ?
Code:
#include<stdio.h> |
Re: Why this program outputs 225 istead of 1 ?
Precedence is not working as you expect with the MACROS.
Try Code:
printf("%d", 225/(SQR(15))); |
Re: Why this program outputs 225 istead of 1 ?
The macro is substituted like the following ,
Code:
#include<stdio.h> |
Re: Why this program outputs 225 istead of 1 ?
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)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;Code:
#define SQR(x) (pow((x),2)) |
| All times are GMT +5.5. The time now is 22:42. |