Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comma as separator in variable initialization (not as operator)

This seems such a simple question, but something I've not examined for ages in my own style... When initializing variables separated by a comma, I've assumed the following to be an unsafe practice:

unsigned int n = foo.size, nxn = n * n;

Since I don't really ever use the comma operator as such for syntactic sugar, etc; but rather to indicate that two expressions are independent - as a kind of implicit commentary on 'fine-grained parallelism' (or expression independence), that often makes code a bit more terse, e.g.,

if (<some condition>)
    a = true, b = value;

rather than requiring {} scope for semi-colon separated expressions.

But my question is really in re-examining the variable initialization case. Have I been incorrect in my assumption that nxn can't be relied on to be initialized as expected? Or have I been laboring under a misinterpretation all this time?

like image 497
Brett Hale Avatar asked Jan 15 '19 13:01

Brett Hale


People also ask

Is comma a binary operator?

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 C++?

In C and C++, comma (, ) can be used in two contexts: 1) Comma as an operator: The comma operator (represented by the token, ) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type).

How do you use commas in codes?

The comma operator ( , ) evaluates each of its operands (from left to right) and returns the value of the last operand. This lets you create a compound expression in which multiple expressions are evaluated, with the compound expression's final value being the value of the rightmost of its member expressions.

How is comma operator useful in a for loop in C?

The comma operator will always yield the last value in the comma separated list. Basically it's a binary operator that evaluates the left hand value but discards it, then evaluates the right hand value and returns it. If you chain multiple of these they will eventually yield the last value in the chain.


1 Answers

Per [dcl.decl]/3

Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself. [...]

we get that

unsigned int n = foo.size, nxn = n * n;

is the same as

unsigned int n = foo.size;
unsigned int nxn = n * n;

There is a note with exceptions for other rules like auto or if a name shadows the type but those don't apply in this case.


Be very wary with pointers if you put multiple variables on a single line

int * foo, bar;

does not give you two pointers. Instead, foo is a pointer and bar is an int. You would need

int * foo, * bar;

to get two pointers. For this reason I would prefer to use

int * foo;
int * bar;

and pay the extra keystorkes for safeties sake.

like image 134
NathanOliver Avatar answered Oct 05 '22 16:10

NathanOliver