Like everyone else, I assume that the line you're wondering about is:
int array1 = {1,2,3,4,5};
In this case, there is no difference between C and C++; the line is arguably legal in both languages, but it doesn't mean what you might think. In both C and C++, there is a statement to the effect that if the type is a scalar type (int
is), then the contents of the {...}
must be a single expression. And 1,2,3,4,5
can be interpreted as a single expression (with the comma operator); something like:
int array1 = 1, 2, 3, 4, 5;
is clearly legal.
It's somewhat ambiguous, however, since in both languages, the grammar for this type of initialization makes the ,
punctuation, and not an operator. So it is a question of interpretation; is the statement that the contents must be a single expression a constraint on the grammar (which would cause the comma to become an operator), or a constraint on the results of evaluating the specified grammar. My intuitive feeling is that the second is the intent, and that the statement should result in an error. But the difference isn't between C and C++, but between the way the authors of the compilers are interpreting the standard.
EDIT:
On rereading a little closer: in the C++ standard, it explicitly says that
If T is a scalar type, then a declaration of the form
T x = { a };
is equivalent to
T x = a;
Which doesn't leave much wriggle room: in C++, the statement seems clearly legal; it's only in C where there is some ambiguity.