confused

Discussion in 'C Programming' started by chump1708@yahoo.com, Jan 17, 2006.

1. Guest

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

, Jan 17, 2006

2. Guest

Ok...got it...its bcoz
4+5*4+4.............

, Jan 17, 2006

3. GuillaumeGuest

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

Guillaume, Jan 17, 2006
4. Guest

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

, Jan 17, 2006
5. Guest

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

, Jan 17, 2006
6. Xiaodong XuGuest

"" <> 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, Jan 17, 2006
7. Xiaodong XuGuest

"" <> 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??

Xiaodong Xu, Jan 17, 2006
8. GuillaumeGuest

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

Guillaume, Jan 17, 2006
9. Martin AmbuhlGuest

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

Martin Ambuhl, Jan 17, 2006
10. Jordan AbelGuest

On 2006-01-17, <> 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))

Jordan Abel, Jan 17, 2006
11. Emmanuel DelahayeGuest

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;
}

--
A+

Emmanuel Delahaye

Emmanuel Delahaye, Jan 17, 2006
12. Jack KleinGuest

On 17 Jan 2006 06:27:03 -0800, ""
<> wrote in comp.lang.c:

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

> and my guess was 81...but its not...

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

Jack Klein, Jan 18, 2006