-3
int main(){
    int x;
    int y = 1;
    int minNumber = min(x,y);
    cout << minNumber;
    return 0;
}

I know x and y are different initialization. But I do not know what excatly happen when I call min() function. Thanks for your time.

xskxzr
  • 12,442
  • 12
  • 37
  • 77
  • 4
    This is undefined behaviour due to using the value of an uninitialized variable – M.M Mar 30 '20 at 03:31

2 Answers2

2

It is undefined behavior because x is uninitialized non static local variable.

srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25
  • 2
    The bit about "is undefined" is correct. "contains random value" is misleading. – eerorika Mar 30 '20 at 03:40
  • "contains any value" is still misleading. Undefined behavior means that the language definition does not tell you what the code does. Yes, typically you get a value that is informally described as "random", but that's not required. Formally, the program can do anything. – Pete Becker Mar 30 '20 at 12:35
1

x is not initialized and hence it would have some value as per what's there in memory location which is assigned for x, by the system. min function's return value will be unpredictable, in this case.

Die-Bugger
  • 166
  • 8