次の簡単なCコードがあります
#define Sqrt(A) A * A
int main(void) {
int A = 10;
int x = Sqrt(A+1);
return 0;
}
どういうわけか、そのように使用すると、A + 1 で x が 21 になり、おそらく 10 + 11 になります。私の質問は、乗算がどのように無視されているのですか? マクロをマクロ テキストで切り替えると、121 という正しい結果が得られます。
ありがとう。
when you call x = MACRO_NAME(A+1); this statement is replace as x = A + 1 * A + 1
in c priority of multiplication is more than addition so it will be 1st executed 1*A which give as A, then A+A+1 will give you result as 21`enter code here`
i.e A+1*A+1
= A+A+1
= 21
for proper answer you need to write Macro as #define MACRO_NAME(A) (A) * (A) which give you result as
121
BODMASルールバディ!! 前の回答が示唆するように。掛け算は、足し算の前に行われます。あなたの操作 Sqrt(A+1) ここで A = 10 は 10+1*10+1 10+10+1 21 に評価されます!!