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

 Page 1 of 1 [ 5 post ]

Relevant Pages