It uses the comma operator, which just evaluates each operand expression sequentially (introducing proper sequence points in between) and returns the last one. Thus your example is actually equivalent to int a = 3;
.
But it is indeed one of the least used operators in C and C++ and not to be confused with the commas used in function call expressions, initializer lists, and all the other places. A not so rare use-case would be multiple increments in for loops (for(...; ...; ++i,++j)
), even though you probably never thought about this actually using a so-called comma operator.
Another interresting use case is when trying to put multiple conceptually related expressions into a single statement (like a return) for the sake of clarity and conciseness, like in an implementation of the good old frexp
with its weird pointer return argument (ignore the fact that proper C++ would just return a pair):
double frexp(double arg, int *exp)
{
if(...)
return *exp=..., result;
...
}
which is much more streamlined than the equivalent
double frexp(double arg, int *exp)
{
if(...)
{
*exp = ...;
return result;
}
...
}