I recently made a syntax mistake by writing this:
b = (float)a*0,1;
instead of:
b = (float)a*0.1;
I am surprised to find out that the first line don't even put a compilation error. I am even more confuse to see that the following line produce compilation error:
float b = (float)a*0,1;
Can someone explain me why the third line is a syntax error but not the first (in fact it is) ?
This ...
b = (float)a*0,1;
... is a statement. Specifically, an expression statement. In the expression within, the comma (,
) is an operator, with operand expressions b = (float)a*0
and 1
. The overall statement is equivalent to
b = (float)a*0; 1;
, and the comma expression itself evaluates to the value of the second operand (1), which happens to be ignored in this case.
On the other hand, this ...
float b = (float)a*0,1;
... is a (malformed) declaration. The comma here is not the comma operator, but rather a separator between items being declared. This declaration is erroneous because the second item does not contain a declarator designating the item being declared. (The declarator in the first item is b
.)