-5

1.macros has always been difficult. 2.Below is the code and output is 125 and 7....Please elaborate the working

#define mul(x) (x++ * ++x * x++)
#include<iostream.h>

void main()
{
    int a=4,j;
    j=mul(a);
    cout<<j<<endl;
    cout<<a<<endl;
}
PMF
  • 14,535
  • 3
  • 23
  • 49
  • 1
    Your macro is not the problem; the code within it is. You're invoking undefined behavior... research _sequence points_ to learn more. – mah Apr 18 '14 at 17:08
  • 2
    This is not a "we solve your homework" site. Nobody's ever going to write something like this in production code. (I really hope so!!) – PMF Apr 18 '14 at 17:08
  • There's nothing really to elaborate besides the result being undefined behavior. The compiler is free to return any number (or, indeed generate code that crashes) – Joachim Isaksson Apr 18 '14 at 17:10
  • Not again a person writing daft code in another form that has been answered countless times. Just do not write silly code that is difficult to understand! Makes it difficult from the outset so feel sorry for the poor idiot that comes back in a few years to maintain it – Ed Heal Apr 18 '14 at 17:10
  • This has nothing to do with macros. It has everything to do with a question that's been asked here about 2,000 times. – Lightness Races in Orbit Apr 18 '14 at 17:10
  • [C operator precedence](http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm) – ZivS Apr 18 '14 at 17:12
  • 1
    @ShaZiv: This has nothing to do with operator precedence. – Fred Larson Apr 18 '14 at 17:14
  • @FredLarson maybe the more interesting part of the question doesn't have to do with it, but for elaboration he should know that `++` comes before `*` (Dereferencing) which comes before `*` (Multiplication) – ZivS Apr 18 '14 at 17:27
  • @ShaZiv: Which all doesn't matter because the expression is riddled with undefined behavior, making the precedence moot. – Fred Larson Apr 18 '14 at 17:30
  • @FredLarson, I accept what your'e saying – ZivS Apr 18 '14 at 17:32
  • as undefined behaviour is result of wrong construction of statement, so what are the rules of right construction of statement – Ravi Allishe Apr 18 '14 at 17:50

2 Answers2

0

Your program results in undefined behaviour. j could be anything.

Read more here: Undefined behavior and sequence points

Community
  • 1
  • 1
Danvil
  • 22,240
  • 19
  • 65
  • 88
  • as undefined behaviour is result of wrong construction of statement, so what are the rules of right construction of statement – Ravi Allishe Apr 18 '14 at 17:50
-1

What I believe is happening is this.

  1. Set a to 4.
  2. mul is called and a is passed in.
  3. The second multiplication (++x) has the prefix incrementer, so x (a) is increased by 1. a is now 5.
  4. The multiplication now happens. Since the first and last have the postfix incrementer, nothing happens until after the multiplication happens. So we have 5 * 5 * 5 = 125.
  5. Since the multiplication is done now, the postfix incrementers happen which makes x (a) 6 and then 7.
  6. j then equals 125.
  7. ???
  8. Profit!

EDIT: This is explaining the behavior that he is experiencing. I understand that this won't happen in every case.

Josh Braun
  • 490
  • 2
  • 16
  • You skipped the part where the answer is not necessarily going to be the same on other systems, or other versions of the compiler. – mah Apr 18 '14 at 17:16
  • True...my bad. Multitasking so kinda just answered what his was doing. – Josh Braun Apr 18 '14 at 17:17
  • The order of evaluation is not defined: "Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression." §5/4 – Danvil Apr 18 '14 at 17:19