0
int a = (1, 2, 5);
printf("a=%d\n", a);

in C, this would print 5.

Can someone explain why 5 is getting printed and in which situation this syntax used?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Yogeswaran
  • 357
  • 1
  • 13

2 Answers2

1

The comma operator in C is a sequence point which means that the expressions separated by the comma are executed from left to right. The value of the whole expression is the value of the rightmost expression, in your case 5, which gets assigned to the variable p.

Sumit Gemini
  • 1,836
  • 1
  • 15
  • 19
0

Comma Operator in C has Lowest Precedence i.e it is having lowest priority so it is evaluated at last.
Comma operator returns the value of the rightmost operand when multiple comma operators are used inside an expression.

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225