the postfix & prefix dilemma

int main()
{
int a=2,b,d,i=0,e=0;
b=++a;
d=a=(a++)+(++a);
printf("%d\t%d\n",a,d);
e=++i + ++i + ++i;
printf("%d\n",e);
getch();
return 0;
}

output:

9 8

7

Can any1 please explain me how thesee expressions are evaluated? please help me.

This is undefined behavior.

Quoting from C standard -

The order of evaluation of the
operands is unspecified. If an attempt
is made to modify the result of an
assignment operator or to access it
after the next sequence point, the
behavior is undefined.

Basically this means, if you modify the same variable in a single statement more than once, the output is unpredictable, and depends on compilers’ implementation.

Of course such ambiguous code should be avoided at all times. And also you should avoid the book/website/friend who is giving that sort of code.

1 Like

#include
#include
#include
#include
#define flag β€˜#’

using namespace std;

bool isOperator(char c)
{
if(c==’+’ || c==’-’ || c==’*’ || c==’/’ || c==’^’ || c==’$’)
return true;
else
return false;
}

int main()
{
stack stk;
char postfix[30], prefix[30];
int j=0,len;
cout<<"Input a postfix expression: ";
cin>>postfix;
len = strlen(postfix);
for(int i=len-1;i>=0;i–)
{
if(isOperator(postfix[i]))
stk.push(postfix[i]);
else
{
prefix[j++] = postfix[i];
while(!stk.empty() && stk.top()==flag)
{
stk.pop();
prefix[j++] = stk.top();
stk.pop();
}
stk.push(flag);

	}
}
prefix[j] = 0;
reverse(prefix, prefix + len);
cout<<"The prefix expression is: "<<prefix;
return 0;

}