From: | |
Date: | 18 Feb 2001 at 01:33:33 |
Subject: | Re: "#define" for code |
To: amiga-c@yahoogroups.com
From: Andrew Crowe <andrewcrowe@enterprise.net>
Date sent: Sat, 17 Feb 2001 19:00:58 +0000
Send reply to: amiga-c@yahoogroups.com
Subject: [amiga-c] "#define" for
> Hi Everybody,
>
> Ok, I know that using #define var_name xxx replaces all the
> "var_name"s in the code with xxx before compiling (so it's faster then
> using a variable), and I /think/ you can do it with piece of code
> like:
>
>
> #define code[a,b,c]{a=b*c+a}
>
> so that if you had the line:
>
> code[var1,var2,6]
>
> the compiler would convert that to
>
> var1=var2*6+var1
>
> before compiling (thus being faster thensubroutines).
>
>
> The only problem is I don't know the syntax for the define, and that
> example there didn't work.
#define MYMACRO(zza,zzb,zzc) {(zza)=(zzb)*(zzc)+(zza);}
would do the job described above. The reason I've used zza, zzb and
the like is because I'm paranoid. :P Something like a and b and so on
is easy to put in the code in the macro where you don't intend to,
whereas zza and zzb etc are less likely to appear in any code you'd
place in the macro definition and so don't accidentally do some
replacement where it wasn't intended.
The parenthesis around the terms is to prevent replacement on things
like:
MYMACRO(myvar,another+5,0); doing unexplained things. Once expanded,
this becomes...
(myvar)=(another+5)*0 + (myvar) ... rather than...
myvar=another + 5 * 0 + myvar;
which if I remember C's precedence rules correctly (which I'm not
confident about) causes a different result to that intended.
The problem is that there are cases where this will behave strangely
compared to a function (for instance, passing an argument with side
effects (like foo++) or doing something like putting a function in
place of an argument which appears twice in the macro). As long as
you avoid these cases it'll work correctly.
It's a good idea to use some convention to identify it as a macro so
that you know just by looking at it the things you need to avoid. The
common convention is to use all capitals... but not everyone likes it.
Ian Woods
------------------------ Yahoo! Groups Sponsor ---------------------~-~>
eGroups is now Yahoo! Groups
Click here for more details
http://click.egroups.com/1/11231/0/_/451227/_/982456140/
---------------------------------------------------------------------_->