"If the type of the operand is a variable length array
type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant."
But what does it mean to evaluate the operand?
If the operand is the name of an object:
int vla[n];
sizeof n;
what does it mean to evaluate `n`? Logically, evaluating it should access the values of its elements (since there's no array-to-pointer conversion in this context), but that's obviously not what was intended.
And what about this:
sizeof (int[n])
What does it mean to "evaluate" a type name?
It's not much of a problem in practice, but it's difficult to come up with a consistent interpretation of the wording in the standard.
> If the operand is the name of an object [...] what does it mean to evaluate `n`?
When you say "n", syntactically, you have a primary expression that is an identifier. So you follow the rules for evaluating an identifier, which will produce the value. C doesn't describe it very well, but the value of the expression is the value of the object. In terms of how it is implemented in actual compilers, this would mean issuing a load of the memory location, which is dead unless `n` is a volatile variable.
Sorry, in the first example I meant to write (adding a declaration and initialization for n):
int n = 42;
int vla[n];
sizeof vla;
not `sizeof n`). (It doesn't look like I can edit a comment.)
Logically, evaluating the expression `vla` would mean reading the contents of the array object, which means reading the value of each of its elements. But there's clearly no need to do that to determine its size -- and if you actually did that, you'd have undefined behavior since the elements are uninitialized. (There are very few cases where the value of an array object is evaluated, since in most cases an array expression is implicitly converted to a pointer expression.)
In fact the declaration `int vla[n];` will cause the compiler to create an anonymous object, associated with the array type, initialized to `n` or `n * sizeof (int)`. Evaluating `sizeof vla` only requires reading that anonymous object, not reading the array object. The problem is that the standard doesn't express this clearly or correctly.
The standard says:
"If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant."
But what does it mean to evaluate the operand?
If the operand is the name of an object:
what does it mean to evaluate `n`? Logically, evaluating it should access the values of its elements (since there's no array-to-pointer conversion in this context), but that's obviously not what was intended.And what about this:
What does it mean to "evaluate" a type name?It's not much of a problem in practice, but it's difficult to come up with a consistent interpretation of the wording in the standard.