Velocity Reviews > confused

# confused

chump1708@yahoo.com
Guest
Posts: n/a

 01-17-2006
#define square(a) (a*a)
main()
{
printf("%d",square(4+5));
}

can anyone tell me why the answer is 29...I know its an inline function
and my guess was 81...but its not...

chump1708@yahoo.com
Guest
Posts: n/a

 01-17-2006
Ok...got it...its bcoz
4+5*4+4.............

Guillaume
Guest
Posts: n/a

 01-17-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> #define square(a) (a*a)
> main()
> {
> printf("%d",square(4+5));
> }
>
> can anyone tell me why the answer is 29...I know its an inline function
> and my guess was 81...but its not...

Because the macro gets expanded as following:

square(4+5) -> (4+5*4+5)

Guess what, it's 29. Operators have prority.

Typical macro definition mistake. Can you find how to modify your macro
so as to avoid this kind of problem? Oh, and even when this mistake is
fixed, beware of the side-effects of macro invokation...

chump1708@yahoo.com
Guest
Posts: n/a

 01-17-2006
yes....here it is...
#define square(a) ((a)*(a))
> main()
> {
> printf("%d",square(4+5));
> }

chump1708@yahoo.com
Guest
Posts: n/a

 01-17-2006
Ok...what are macro arguments?? n how do they differ from the normal
macro definition??

Xiaodong Xu
Guest
Posts: n/a

 01-17-2006
"(E-Mail Removed)" <(E-Mail Removed)> writes:

you may define like this:

#define square(a) ((a)*(a))

be aware of the use of parentheses in macros definition

> #define square(a) (a*a)
> main()
> {
> printf("%d",square(4+5));
> }
>
> can anyone tell me why the answer is 29...I know its an inline function
> and my guess was 81...but its not...

Xiaodong Xu
Guest
Posts: n/a

 01-17-2006
"(E-Mail Removed)" <(E-Mail Removed)> writes:

in fact I think there is no arguments for macro definition.
it's not like function definition for it is handled by precompiler.
the compiler just substitute the right part of macro defition for
the left part of macro in the program before compiling.

> Ok...what are macro arguments?? n how do they differ from the normal
> macro definition??

Guillaume
Guest
Posts: n/a

 01-17-2006
(E-Mail Removed) wrote:
> Ok...what are macro arguments?? n how do they differ from the normal
> macro definition??

Macros only define text substitution. The possibilities for side-effects
are nearly endless.

Typical example with your macro: square(i++)

Not only will i be incremented twice, but the result will be undefined
(if I'm not mistaken), because the order of evaluation in expressions
is undefined (with some exceptions, such as logical operators).

Martin Ambuhl
Guest
Posts: n/a

 01-17-2006
(E-Mail Removed) wrote:
> #define square(a) (a*a)
> main()
> {
> printf("%d",square(4+5));
> }
>
> can anyone tell me why the answer is 29...I know its an inline function
> and my guess was 81...but its not...

#define square(a) (a*a)
square(4+5);
expands to
4 + 5 * 4 + 5;
which evaluates as
4 + 20 + 5; -> 29

You want
#define square(a) ((a)*(a))

Jordan Abel
Guest
Posts: n/a

 01-17-2006
On 2006-01-17, (E-Mail Removed) <(E-Mail Removed)> wrote:
> #define square(a) (a*a)
> main()
> {
> printf("%d",square(4+5));
>}
>
> can anyone tell me why the answer is 29...I know its an inline function
> and my guess was 81...but its not...
>

Because 4+5*4+5 is 29.

try defining it as ((a)*(a))