Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

order of execution in an expression with commas in C++ [duplicate]

Tags:

c++

It is my understanding that the term j = i will be executed before ++i in the statement

j = i, ++i;.

Does the C++ standard guarantee that j = i will be executed before ++i in the loop

for (auto i = std::next(begin), j = begin; i!= end; j= i, ++i)?

like image 769
Catriel Avatar asked Nov 23 '16 20:11

Catriel


People also ask

How does comma operator work in C?

The comma operator in c comes with the lowest precedence in the C language. The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.

Can we use comma in for loop in C?

The commas you see in C for loops are not part of the syntax of the for loop specifically. They are just manifestations of the comma operator. Commas are major separators between arguments in function calls and between parameters in function declarations, but semicolons are not used.

What is comma expression in loop?

You can use the comma operator when you want to include multiple expressions in a location that requires a single expression. The most common usage of this operator is to supply multiple parameters in a for loop.


1 Answers

The comma operator introduces a sequence point and, as such, this behavior is guaranteed by the C++ standard.

like image 198
Sam Varshavchik Avatar answered Sep 25 '22 06:09

Sam Varshavchik