jason <ji***@notmal.comwrites:
I learned my lesson about passing pointers, but now I have a question
about macros.
Why does the function work and the MACRO which is doing the same thing
on the surface, does not work in the following small example ?
#include <stdio.h>
#define PERCENTAGE(a, b) (((a - b) / a) * 100.0)
double percent(double a, double b) {
return ((a - b) / a) * 100.0;
}
int main(void) {
printf("%g%%\n", percent(1000, 100));
printf("%g%%\n", PERCENTAGE(1000, 100));
return 0;
}
Others have explained why your macro doesn't work. I'll offer some
additional advice.
You get 100 points (on a scale of 0 to whatever the heck) for posting
a complete program that exhibits the problem, but you lose 20 points
for merely telling us that it "does not work" without explaining *how*
it doesn't work. In this case, it was easy enough to figure out the
problem, but in general you should tell us *how* it didn't work --
i.e., what output you expected, what output you got, and perhaps why
you think your expected output is right and what you got is wrong.
A good resource for this kind of thing is
<http:°www.catb.org/~esr/faqs/smart-questions.html>.
As for your macro, leaving aside the type problem, I see two other
potential issues. First, it always evaluates the first argument
twice. This isn't necessarily a problem; the all-caps name warns
users that this is a macro, and this kind of thing might happen.
Second, for a function-like macro that expands to an expression, you
should always fully parenthesize the entire expression (which you did)
*and* each occurrence of an argument (which you didn't). Specifically:
#define PERCENTAGE(a, b) ((((a) - (b)) /(a)) * 100.0)
(Note: it's not *always* strictly necessary, but I find it much easier
to be consistent than to remember the cases where it isn't.)
This doesn't matter if the arguments are simple identifiers or
constants, but imagine what happens if the arguments are more complex
subexpressions. Macro expansion knows nothing about operator
precedence; it just blindly expands each argument in place. Here's an
example of how failing to parenthesize sufficiently:
#include <stdio.h>
#define SIX 1+5
#define NINE 8+1
int main(void)
{
printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
return 0;
}
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"