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;
}
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;
}
Your program results in undefined behaviour. j could be anything.
Read more here: Undefined behavior and sequence points
What I believe is happening is this.
a to 4.mul is called and a is passed in.++x) has the prefix incrementer, so x (a) is increased by 1. a is now 5.x (a) 6 and then 7.j then equals 125.EDIT: This is explaining the behavior that he is experiencing. I understand that this won't happen in every case.