i = 1, 2, 3, 4, 5;
this actually assigns 1 to i.
*****I wonder if this type of assignment is actually useful somewhere?*****
Do you know some application of this syntax?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s not a “type of assignment”. The comma operator binds very loosely, looser than assignment. So you’ve written the equivalent of:
Integer literals in void contexts are useless (except maybe for avoiding warnings in macros that do nothing in certain cases, like assert), so no, there’s no use for exactly this syntax – the need for an expression which sets i to 1 and evaluates to 5 is pretty limited, and even if you found a case for that, the 2,3,4 are redundant.
More useful could be
i = 1, code_that_actually_does_something;. The most frequent use of the comma operator is to sneak in multiple side-effects in a context where you’re not allowed multiple statements, such as in “if” and “while” conditions, or macros that have to evaluate as expressions.