Macros perform direct text (or more accurately, token) substitution. So this:
mod(y-x)
Is exactly the same as this:
(y-x>=0?y-x:-y-x)
Notice that the last part is -y-x
, i.e. the negation of y
minus x
, while what you wanted was -(y-x)
. This is a prime example of why macro arguments should always be placed in parenthesis as follows:
#define mod(a) ((a)>=0?(a):-(a))
When you want to see what your macro actually does, look at your code after the preprocessor has actually done its job:
gcc -E file.c
clang -E file.c
or clang --preprocess file.c
cl.exe /E file.c
, cl.exe /P file.c
, or cl.exe /EP file.c
for those on windows
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…