-5

Can anybody tell me what is happening in this assignment statement? It was found in some code in a textbook. Somehow this is where val is initialized and without it it does not compile. INT_MAX is coming from The body of the code shouldn't matter too much but I've never seens two values to the right of an assignment operator

int lru = INT_MAX, val;

Edit: People need to relax. This is getting downvotes WAY after it was answered. (I lost my ability to upvote which was not cool) I had an honest question that my TA nor my professor could answer. Thats because normally people write

int val

int lru = INT_MAX

on separate lines. Or they define them both as ints int val, lru and then assign lru = INT_MAX

And at the very least put all the assignments/constructions after the definitions int val, lru = INT_MAX It was an honest question. I did research, I looked through previous questions and answers and couldn't find what I was looking for.

KickingAustin
  • 47
  • 1
  • 8
  • 4
    `int lru = INT_MAX, val;` - this is not a comma operator, this is `lru` and `val` variable definitions. And `lru` has an initialization. – KamilCuk Nov 28 '19 at 21:41
  • 1
    @SlavasupportsMonica that's the wrong duplicate – Thomas Sablik Nov 28 '19 at 21:42
  • @ThomasSablik agree, OP statement about assignment operator confused me. – Slava Nov 28 '19 at 21:43
  • Sorry about the confusion but if its something I've never seen before then I can olny express it in certain terms. Besides the '=' is still an assignment operator isnt it? @KamilCuk says that the statement assigns the value of INT_MAX to lru and val, right? – KickingAustin Nov 28 '19 at 21:51
  • Possible duplicate of [How can I declare and define multiple variables in one line using C++?](https://stackoverflow.com/questions/6838408/how-can-i-declare-and-define-multiple-variables-in-one-line-using-c) – arsdever Nov 28 '19 at 21:54
  • It's not a duplicate simply because I'm not trying to define multiple variables. I'm trying to understand whats happening. My TA didn't even know what it meant because theres an unwritten rule about the order of definitions. If it were written "int val, lru = INT_MAX;" it would be far less confusing in my opinion. – KickingAustin Nov 28 '19 at 22:00

1 Answers1

3

It is not an assignment. It is just 2 declarations. First of them is a declaration of lru, which is being constructed with value INT_MAX. The second is val, which remains uninitialized.

arsdever
  • 1,111
  • 1
  • 11
  • 32