Tricky macro question 
Author Message
 Tricky macro question

I'm an experienced programmer who is embarrassed that I could  not answer
the macro question below, which was part of an employment test.
--------------------------
Why do you need the parenthesis on the right?

#define SQUARE(A)       (A) * (A)
---------------------------

Any ideas?

Michael T.



Mon, 18 Nov 2002 03:00:00 GMT  
 Tricky macro question

Quote:
> Why do you need the parenthesis on the right?
> #define SQUARE(A)       (A) * (A)
> Any ideas?

Because A is symbolic.  What if you use your macro to square "a + b"?  Your
macro, without parenthesis, would produce:

a + b * a + b    => mistakenly translated into =>   a + (b * a) + b.

By putting parenthesis around the parameters on the right it allows the
square macro to work with any input.  The above example becomes (a + b) * (a
+ b).

- Rick C. Hodgin



Mon, 18 Nov 2002 03:00:00 GMT  
 Tricky macro question


Quote:
> I'm an experienced programmer who is embarrassed that I could  not answer
> the macro question below, which was part of an employment test.
> --------------------------
> Why do you need the parenthesis on the right?

> #define SQUARE(A)       (A) * (A)
> ---------------------------

> Any ideas?

> Michael T.

If an employer handed me this question, I would suggest to them
that this macro is incorrect and should be correctly worded as

#define SQUARE(A)  ((A)*(A))

Of course, if you asked this in the C newsgroups, they would
have already jumped all over this one.
Randy Hyde



Mon, 18 Nov 2002 03:00:00 GMT  
 Tricky macro question
On Thu, 01 Jun 2000 17:24:20 GMT, "Michael Thal"

Quote:
> I'm an experienced programmer who is embarrassed that I could  not answer
> the macro question below, which was part of an employment test.
> --------------------------
> Why do you need the parenthesis on the right?

> #define SQUARE(A)       (A) * (A)
> ---------------------------

> Any ideas?

> Michael T.

Why are you asking a C language question in an assembly language
newsgroup?

Jack Klein
--
Home: http://jackklein.home.att.net



Tue, 19 Nov 2002 03:00:00 GMT  
 Tricky macro question
: I'm an experienced programmer who is embarrassed that I could  not answer
: the macro question below, which was part of an employment test.
: --------------------------
: Why do you need the parenthesis on the right?

: #define SQUARE(A)       (A) * (A)
: ---------------------------

: Any ideas?

: Michael T.

I think they are required to compel evaluation.  If 1+2 is substituted for
A, the result is 1+2*1+2, which comes to 5, rather than (1+2)*(1+2)=9

Best regards, Bill Thomson



Tue, 19 Nov 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. tricky question about R5Rs macros

2. Question about a macro-defining macro

3. Tricky DLL question

4. Tricky indexing question (probably impossible)

5. Tricky install question

6. Tricky VRML question

7. tricky DEFINE-SYNTAX question

8. Tricky interpreter initialization question..

9. tricky question

10. Some tricky questions on standard conformance

11. Basic ForTran Question (but tricky!!)

12. Tricky scoping question for poor man's debugger

 

 
Powered by phpBB® Forum Software