Re: C Test Incorrectly Uses printf() - Please Confirm



On Aug 11, 4:14 pm, Keith Thompson <ks...@xxxxxxx> wrote:
Shao Miller <sha0.mil...@xxxxxxxxx> writes:
On Aug 10, 12:16 pm, Keith Thompson <ks...@xxxxxxx> wrote:
Shao Miller <sha0.mil...@xxxxxxxxx> writes:

[...]> I suppose it just seems counter-intuitive to me that function
arguments be treated the same as sub-expressions for the computation
of a single value, instead of as independent expressions not part of a
larger expression.

[...]

A function call is an expression (6.5.2.2).
Yes of course, and that is why I've conceded.

So how about:

int a = 1;
printf("%d", (int){ ++a }, (int){ a + 5 });

?  There appear to be 3 full expressions in the second line, there.
Does 6.5p2 apply there, too?

Hmm.  (int){ ++a } and (int){ a + 5 } are compound literals.
The syntax of a compound literal is:

    ( type-name ) { initializer-list }
    ( type-name ) { initializer-list , }

Following the syntax of initializer-list, we see that ++a and a + 5 are
both initializers.  6.8p4 seems to say that both that an initializer is
a full expression, and that these particular initializers are not.
Here's the full paragraph:

    A _full expression_ is an expression that is not part of another
    expression or of a declarator.  Each of the following is a full
    expression: an initializer; the expression in an expression
    statement; the controlling expression of a selection statement
    (if or switch); the controlling expression of a while or do
    statement; each of the (optional) expressions of a for statement;
    the (optional) expression in a return statement. The end of a
    full expression is a sequence point.

The first sentence is the definition of the term "full expression".
By this definition, since ++a and a + 5 are part of larger
expressions, they clearly aren't full expressions.  But the next
sentence says that an initializer is a full expression.

The wording is unchanged, or nearly so, from C90, which didn't
have compound literals; initializers appeared only in declarations.
I suggest that the authors neglected to update this paragraph when
compound literals were added, and that these initializers are not
(or should not be) considered to be full expressions; thus the
behavior is undefined.
That makes complete sense. Essentially, "If an initializer appears
within an expression, it is not a full expression."
.