In what order does i = (i, ++i, 1) + 1; run?

Asked 2 years ago, Updated 2 years ago, 112 views

#include <stdio.h>

int main(void) {
  int i = 5;
  i = (i, ++i, 1) + 1;
  printf("%d\n", i);
  return 0;
}

I'm Newbie who is studying about the Undefined Behavior.

I was experimenting with this and that with operators, and I made this code If you run the code, you get 2, but in the code I made, there's no decreasing operator, so you don't get 2. Why did I get 2?

And if you execute that, you get this warning. Why?

px.c:5:8: warning: left-hand operand of comma expression has no effect

  [-Wunused-value]   i = (i, ++i, 1) + 1;
                        ^

c expression compiler-warning comma-operator operator

2022-09-21 15:21

1 Answers

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.