Re: Macros
- From: roberson@xxxxxxxxxxxxxxxxxx (Walter Roberson)
- Date: Tue, 28 Nov 2006 07:57:39 +0000 (UTC)
In article <1164698162.284936.137360@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Harry <gehariprasath@xxxxxxxxx> wrote:
I have a piece of code as
# define prod(a,b)=a*b
main()
{
int x=2;
int y=3;
printf("%d",prod(x+2,y-10));
}
will the macro be evaluated as (2+2)*(3-10)=4* -7 =-28.
No, each dummy argument will be *textually* dropped into place exactly
as specified. Thus, your line would expand to
printf("%d",x+2*y-10);
This is why you very often see () in macros, such as
# define prod(a,b) ((a)*(b))
Some comments:
- don't use = in the macro definition
- remember to include stdio.h
- look in the FAQ for information about why you want to use a different
way of declaring main
- try adding a \n after the %d or else you might not get -any- output
- notice that when I defined prod(a,b) I enclosed the (a)*(b) in ().
That's because whatever is textually right next to prod(a,b) might happen
to have a higher compilation priority.
- return a value from main. You omitted the 'int' qualifier of
return type, which is not legal in C99, so you cannot take advantage
of the C99 requirement that not returning anything be equivilent
to returning 0; hence you must return something yourself.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
.
- Follow-Ups:
- Re: Macros
- From: Peter "Shaggy" Haywood
- Re: Macros
- References:
- Macros
- From: Harry
- Macros
- Prev by Date: [OT] Re: Reading a string of unknown size
- Next by Date: Pointers and nested structures
- Previous by thread: Re: Macros
- Next by thread: Re: Macros
- Index(es):
Relevant Pages
|
|