1

How does this logic work: k+=(x=5,y=x+2,z=x+y);

How it will give result k == 22. When I initialize the value of k = 10

#include <stdio.h>
int main()
{
    int x,y,z,k=10;
    k+=(x=5,y=x+2,z=x+y);
    printf("value of k %d ",k); //why it will show value of k =22
    return 0;
}
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • 3
    The value of `(x=5,y=x+2,z=x+y)` is `z`, the rightmost operand of the comma operator. `z` has the value of `12` (`5 + (5+2)`) which, added to the initial `10` in `k` gives `22`. – pmg Jan 20 '21 at 14:24
  • 3
    Just a comment: Even though I'm an expert on C, if you asked me what the expression `k+=(x=5,y=x+2,z=x+y);` did, I would say "I don't know and I don't care." That expression is so crazily unrealistic that it's basically meaningless. Not only is there no place for it in "real" code, I believe it's a waste of time to even attempt to learn from it. Your instructor, unfortunately, may feel otherwise. – Steve Summit Jan 20 '21 at 14:32
  • 1
    @SteveSummit Not necessarily a waste of time. You might find such... cleverness... in the wild, and it may be necessary (or at least desirable) to rewrite it in a more sane fashion without affecting the semantics of the code. (Yes, it would be even better to have a specification for what the code does or should do, but failing that, you don't necessarily want to change the behavior in the name of making the code more maintainable.) – chepner Jan 20 '21 at 14:37
  • 1
    @SteveSummit - On the contrary - It serves as a great illustration (therefore learning tool) of how sequence points work in C. The fact that you don't like it is beside the point. – ryyker Jan 20 '21 at 14:40
  • 1
    @chepner Sure, but: we're not talking about an experienced programmer debugging bad code in the wild. We're talking, I think, about a beginning programmer learning C for the first time. Using code like this in a pedagogical way is the moral equivalent, I believe, of teaching a child to walk while hitting him on the head with a heavy stick while he tries. Now, it's true, a soldier or a bodyguard is going to have to be able to perform while being hit with a stick, or worse. But that doesn't mean a child needs this skill, or is well served by the attempt. – Steve Summit Jan 20 '21 at 14:41
  • 1
    @ryyker It's not as bad as some. But: Why `+=`? If the expression were `k = (x=5, y=x+2, z=x+y)` I'd probably agree with you. See also my reply to chepner above. There is just way too much of this intentional obfuscation in introductory education. It's the equivalent of hazing: "I had to put up with this when I learned C, so you should, too." – Steve Summit Jan 20 '21 at 14:44
  • @ryyker How is this not a dupe? "Teach me about operator x so I don't have to read a book" questions are generally not valuable and have been asked countless times before. – Lundin Jan 20 '21 at 14:50
  • @SteveSummit The comma operator has a few valid uses in function-like macros returning a value etc. But I agree that it's definitely not something beginners need to know and care about. – Lundin Jan 20 '21 at 14:52
  • @pmg Re "*`z`, the rightmost operand of the comma operator*", Technically, that would be the final `=`, not `z`. `z` is an operand of the assignment, and the assignment is the operand of the comma operator. But the assignment returns `z`, so close enough :) – ikegami Jan 20 '21 at 14:53
  • @Lundin I have no objecting to teaching, learning, or using the comma operator. It's teaching it while simultaneously hitting the student over the head with a heavy `+=` that I'm objecting to. :-) – Steve Summit Jan 20 '21 at 14:55
  • @SteveSummit Yeah I do agree with the hit student with a stick analogy. There's a lot of things that don't belong in beginner classes. I'd like to see more "this is how you use a debugger" and less "what is the result of i++++i". – Lundin Jan 20 '21 at 15:01
  • @ikegami - Your characterization of OP reasoning for posting this shows impressive ability to read human intent. That notwithstanding I see much more here then simply [What does the , operator do in C?](https://stackoverflow.com/questions/52550/what-does-the-comma-operator-do). As indicated several times in comments regarding the difficulties in this post that include elements of obfuscation in the statement. (eg. see comments surrounding _Chepner's_ comment: _"You might find such... cleverness... in the wild..."_, that point alone is distinctive enough.... – ryyker Jan 20 '21 at 15:14
  • @ikegami ...Beside that, the accepted answer IMO is a clearer presentation of when running into something like this, how to deal with it. Excellent illustration as a learning tool for this topic (which include sequence points, and interpreting obfuscation.) – ryyker Jan 20 '21 at 15:16
  • Oh, it's 100% a waste of time, except as a homework answer. No professional writes code like that because they know that they will have to debug it later. Questions with 'clever' code turn up frequently on this tag, and they should all be closed. Untangling deliberately-undebuggable code is not helpful to current and future users, (except for copypasta homework answers). – Martin James Jan 20 '21 at 15:32

1 Answers1

6

In the right side of the assignment statement there is used the comma operator (sequentially two times from left to right). Its value is the value of the last operand. So this statement

k+=(x=5,y=x+2,z=x+y);

that can be for clarity rewritten like

k+=( ( x = 5, y = x + 2 ), z = x + y );

in fact is equivalent to the following set of statements

x = 5;      // x is set to 5
y = x + 2;  // y is set to 7
z = x + y;  // z is set to 12
k += z;     // k is set to 22

From the C Standard (6.5.17 Comma operator)

2 The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335