#define SQR(x) x * x

int main()

	printf("%d", 225/SQR(15));


why this program outputs 225 instead of 1 (according to calculation).

8 Years
Discussion Span
Last Post by aman rathi

this is a classic error. the macro just replaces the code in the program with your #define'd value verbatim. therefore, it's calculating according to the order of operation: 225 / 15 * 15 , which is 225.

change your #define to

#define SQR(x)     (x * x)


Edited by jephthah: n/a

#define SQR(x)     (x * x)

Careful now... what happens if you run SQR(3+2)? (3 + 2 * 3 + 2) = 11 Every time a parameter appears in a macro, it should be surrounded by parentheses.

#define SQR(x)   ((x)*(x))

Edited by IsharaComix: n/a

Votes + Comments
well, dang. :P

just look at the maths rule BODMAS.
in your program firstly 225 is divided by 15 and after multiplyed by 15 which comes equal to 225. you should use parentheses for desired result.

Votes + Comments
how about reading the thread before chirping in with an exact copy of what was already posted
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.