Why does double x = 0,1; not compile?
-
21-07-2021 - |
Question
double x = 0,1;
doesn't compile (tries on on MSVC9.0). The error is
C2059 syntax error : 'constant'
I do realize that there's a comma there instead of a point, but shouldn't the line above be interpreted as the following?
double x = (0,1); //which is double x = 1;
Incidentally, the initialization compiles successfully with the parentheses.
I was thinking along the lines that operator ,
has a lower precedence than operator =
, but in this case =
is no operator, so this shouldn't be an issue. What syntactic rules determine that
double x = 0,1;
should be illegal?
Solution
During declarations, the comma in the absence of parenthesis is treated as a separator between declarations. For example:
double x = 0, y = 1;
or
double x = 0, y;
What you typed is the equivalent of
double x = 0;
double 1;
Which is obviously not correct.
OTHER TIPS
Well, it's treated as
double x = 0; double 1;
that's why. Just like you'd write
double x = 0, y;
It's the syntax for a declaration, you're just attempting to declare 1
as a variable (which is wrong). Don't think there's much more to it...