0

While creating reverse polish notation calculator. i came across this . Unable to understand why such values were being assigned while debugging , i isolated the problem statements into another simpler program which is as follows and it gives the same output which i am having difficulty understanding . Can anyone explain why is it different from my expected output (or simply how this output was reached). Your help would be highly appreciated!!

#include <stdio.h>
    
int main()
{
    int top_stack =  1;
    printf("%d\t%d\t%d" , top_stack + 1 , top_stack , top_stack++) ;
    return 0;
}

Output :- 3 2 1

I was expecting the output to be 2 1 1

  • 7
    Hint: What order are the arguments evaluated in? What does the C standard require? – tadman Jun 17 '21 at 20:12
  • 1
    Evaluation order of the arguments are undefined but afaik traditionally it's from right to left. So you shouldn't rely on anything about that ordering. – bca Jun 17 '21 at 20:15
  • 1
    @Aakarsh MJ The function has undefined behavior. There is no sequence point in the evaluation of function arguments. – Vlad from Moscow Jun 17 '21 at 20:25
  • Arguments are evaluated from right to left according to C standards hence the output (FOR SELF REFERRENCE). – ChucklesDroid Jun 22 '21 at 04:45

0 Answers0