Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > confused

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
chump1708@yahoo.com
Guest
Posts: n/a
 
      01-17-2006
Ok...got it...its bcoz
4+5*4+4.............

 
Reply With Quote
 
 
 
 
Guillaume
Guest
Posts: n/a
 
      01-17-2006
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...
 
Reply With Quote
 
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));
> }


 
Reply With Quote
 
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??

 
Reply With Quote
 
Xiaodong Xu
Guest
Posts: n/a
 
      01-17-2006
"" <> 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...

 
Reply With Quote
 
Xiaodong Xu
Guest
Posts: n/a
 
      01-17-2006
"" <> 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??

 
Reply With Quote
 
Guillaume
Guest
Posts: n/a
 
      01-17-2006
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).
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-17-2006
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))


 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      01-17-2006
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))
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
confused! Dennis J. Tuchler Firefox 4 05-19-2005 12:04 AM
Thunderbird 1.0 got confused with news 'Captain' Kirk DeHaan Firefox 0 12-24-2004 08:16 PM
Utterly confused - web, sql tables what to use for development? RichGK ASP .Net 1 07-31-2004 05:16 AM
Confused and bewildered. DJ Miller ASP .Net 4 09-04-2003 06:10 PM
Confused. Need Help! Cobra Pilot Perl 1 07-22-2003 03:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57