0

Is there a order which is followed when passing argument to a function in c++? or is it compiler dependent?
I am unable to understand the output what is happening here?!

#include <iostream>

void sum(int a, int b){
  std::cout << "a = " << a << " " << "b = " << b << '\n';
  std::cout << "sum = " << a+b;
  
}
int main(){
  int a=1;
  sum(++a, a++);

  return 0;
}

Output:

a = 3
b = 1
sum = 4

Regards

DK_bhai
  • 337
  • 3
  • 14
  • 2
    If you know it's UB, what exactly are you trying to understand about the output? – cigien Nov 15 '20 at 14:06
  • I wanted to know a little bit about the working as it is surely not providing random values. In this case a is always incremented by 2 and b remains the same as in the main function. Actually I don't really know what UB means, it does not seem that much "undefined", I checked few online compilers also they all provide same output so how is this UB(I am guessing from the dictionary meaning) – DK_bhai Nov 15 '20 at 14:41
  • Actually UB means you can't make *any* claims about what a program will do across different compilers. You *can* ask what it will do for a specific compiler, on a specific architecture, with specific flags, etc. but that's a different question. – cigien Nov 15 '20 at 14:44
  • I am getting same output in online compilers as well are are they all MinGW which I am using? – DK_bhai Nov 15 '20 at 14:48
  • Not at all. Again, UB means anything can happen, and I do mean anything. For example, all compilers might produce the same result for a program with UB. – cigien Nov 15 '20 at 14:52
  • Oh! right that sure is a possibility. Thank You for your time. – DK_bhai Nov 15 '20 at 14:56

0 Answers0