我讀*
(乘法)比/
(除法)有更高的優先權。因此,如果存在與*
和/
兩者的等式,則必須首先發生*
。乘法和除法:奇怪的輸出在c
但我見過一個程序,輸出一些奇怪的
#include<stdio.h>
#define SQUARE(x) x*x
int main()
{
float s=10, u=30, t=2, a;
a = 2*(s-u*t)/SQUARE(t);
printf("Result = %f", a);
return 0;
}
運行此,我認爲輸出是-25,但實際上卻是-100。
當我看到您的解釋是
Step 2: a = 2*(s-u*t)/SQUARE(t); becomes,
// Here SQUARE(t) is replaced by macro to t*t
=> a = 2 * (10 - 30 * 2)/t * t;
=> a = 2 * (10 - 30 * 2)/2 * 2;
=> a = 2 * (10 - 60)/2 * 2;
=> a = 2 * (-50)/2 * 2 ;
/*till here it's OK*/
/*why it divided -50 by 2 before multiplying 2*2 and -50*2 */
=> a = 2 * (-25) * 2 ;
=> a = (-50) * 2 ;
=> a = -100;
任何一個可以解釋一下嗎?
另一個參考:http://msdn.microsoft.com/en-us/library/126fe14k.aspx – Inisheer
這有很多重複,例如,g [宏觀擴展的奇怪行爲](http://stackoverflow.com/questions/15337185/strange-behavior-of-macro-expansion) –