The effect of using a comma instead of a semi-colony in C and C ++

I have repeatedly noticed when refactoring various fragments of code in C and C ++ is used instead of a comma, and not as a colon. Something like that;

int a = 0, b = 0; a = 5, b = 5; 

Where would i expect

 int a = 0, b = 0; a = 5; b = 5; 

I know that C and C ++ allow you to use commas to separate statements (especially outline headers), but what is the difference between the two code snippets? I assume that the comma was left as a result of cutting and pasting, but is it a mistake and does it have an effect?

+50
c ++ c
Jan 18 '10 at 15:25
source share
5 answers

This does not affect the code you posted. In general, a comma separates expressions in the same way as a semicolon, however, if you accept the integer as an expression, the comma operator means that the expression evaluates to the last argument.

Here is an example:

 b = (3,5); 

Will evaluate 3, then 5 and assign the last b. So b == 5. Note: parentheses are important here:

 b = 3, 5; 

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

The comma operator is especially useful for for-loops when your iterator code is not simple i ++, but you need to do a few commands. In this case, the semicolon does not work with for-loop syntax.

+78
Jan 18
source share
β€” -

A comma is an operator that returns a value that is always the 2nd (right) argument, while the semicolon just ends. This allows you to use the comma operator inside other operators or to concatenate multiple operators as one.

Here the function f (x) is called, and then x > y computed for the if statement.

 if( y = f(x), x > y ) 

An example where it was used only to avoid the need for a block

 if( ... ) x = 2, y = 3; if( ... ) { x = 2; y = 3; } 
+20
Jan 18
source share

The comma operator evaluates all operands from left to right, and the result is the value of the last operand.

In most cases, this is useful for for-loops if you want to do a few actions in the "increment" part, for example (reversing the string)

 for (int lower = 0, upper = s.size() - 1; lower < upper; ++lower, --upper) std::swap(s[lower], s[upper]); 

Another example where this could be an option (search for all occurrences in a string):

 #include <string> #include <iostream> int main() { std::string s("abracadabra"); size_t search_position = 0; size_t position = 0; while (position = s.find('a', search_position), position != std::string::npos) { std::cout << position << '\n'; search_position = position + 1; } } 

In particular, they are logical and cannot be used for this condition, since both zero and non-zero can mean that the character was found in the string. On the other hand, with a comma, position = s.find() is called every time a condition is evaluated, but the result of this part of the condition is simply ignored.

Naturally, there are other ways to write a loop:

 while ((position = s.find('a', search_position)) != std::string::npos) 

or simply

 while (true) { position = s.find('a', search_position); if (position == std::string::npos) break; ... } 
+6
Jan 18
source share

one use will be in a game with a code:

 if(x==1)y=2,z=3; if(x==1){y=2;z=3;} 

the first line is shorter, but it seems too confusing to use in regular development

+2
Jan 18
source share

As Frank mentioned how the comma operator is used in your example, it does not cause an error. The comma operator can be misleading for several reasons:

  • it does not occur too often, because it is necessary only in some special situations.
  • There are several other syntactic uses of the comma that may look like a comma operator, but they are not (commas used to separate function parameters / arguments, commas used to separate variable declarations or initializers)

Since this is confusing and often unnecessary, a comma operator should be avoided, except in some very specific situations:

  • it may be useful to perform several operations in one or more for statements that control expressions
  • it can be used in preprocessor macros to evaluate more than one expression in a single expression. This is usually done so that macros can do more than one, and still be a single expression, so the macro will "match" in places that allow only the expression.

The comma operator is a pretty hacky operator by definition - it has to crack 2 things where only one is allowed. It's almost always ugly, but sometimes that's all you have. And this is the only time you should use it - if you have another option, do not use the comma operator.

At the top of my head, I can't think of too many other reasons for using the operator, since you can get a similar effect by evaluating expressions in separate statements in most other situations (although I'm sure someone will comment on another use that I missed).

+2
Jan 18
source share



All Articles