confused

C

chump1708

#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...
 
G

Guillaume

#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...
 
C

chump1708

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

Xiaodong Xu

you may define like this:

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

be aware of the use of parentheses in macros definition
 
X

Xiaodong Xu

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.
 
G

Guillaume

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).
 
M

Martin Ambuhl

#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))
 
J

Jordan Abel

#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))
 
E

Emmanuel Delahaye

(e-mail address removed) a écrit :
#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...
Please post complete code. See how the macro expands. As a rule of
thumb, the parameters of a macro should be braced.

#include <stdio.h>

#define square(a) (a*a)

#define square_fixed(a) ((a)*(a))

int main(void)
{
printf("%d\n", square(4 + 5));
printf("%d\n", square_fixed(4 + 5));
return 0;
}
 
J

Jack Klein

#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

You are wrong, it is not an inline function or any kind of function at
all. It is a macro, and others have explained why it doesn't expand
the way you think.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top