P
Peter Ammon
K.N. King's book "C Programming: A Modern Approach" gives this exercise
in chapter 14, which is about the preprocessor:
The following macro has a subtle problem:
#define ABS(a) ((a)<0?-(a):a)
Give an example that shows why ABS doesn't work,
and show how to fix the problem. You may assume
that the argument to ABS doesn't have a side effect.
The only potential problem I see is when INT_MIN is passed, which I do
not believe to be the solution that King has in mind. Can anyone shed
some light on this problem? Most of the other problems in the chapter
are related to not parenthesizing the arguments properly, but I can't
think of a case where the missing parenthesis could causse a problem in
the above macro.
Thanks.
-Peter
in chapter 14, which is about the preprocessor:
The following macro has a subtle problem:
#define ABS(a) ((a)<0?-(a):a)
Give an example that shows why ABS doesn't work,
and show how to fix the problem. You may assume
that the argument to ABS doesn't have a side effect.
The only potential problem I see is when INT_MIN is passed, which I do
not believe to be the solution that King has in mind. Can anyone shed
some light on this problem? Most of the other problems in the chapter
are related to not parenthesizing the arguments properly, but I can't
think of a case where the missing parenthesis could causse a problem in
the above macro.
Thanks.
-Peter