Effect of using a comma instead of a semi-colon in C and C++

It doesn’t make a difference in the code you posted. In general, the comma separates expressions just like a semicolon, however, if you take the whole as an expression, then the comma operator means that the expression evaluates to the last argument.

Here’s an example:

b = (3, 5);

Will evaluate 3, then 5 and assign the latter to b. So b = 5. Note that the brackets are important here:

b = 3, 5;

Will evaluate b = 3, then 5 and the result of the whole expression is 5, nevertheless b == 3.

The comma operator is especially helpful in for-loops when your iterator code is not a simple i++, but you need to do multiple commands. In that case a semicolon doesn’t work well with the for-loop syntax.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)