Paul Hsieh said:
C is an uncommon language in that it does not require a comparison
operator in its if statements, and that all statements have side-
effects.
ANSI X3.159-1989 (aka C89)
Appendix A Language Syntax Summary
A.2.3 Statements
(3.6) statement:
labeled-statement
compound-statement
expression-statement
selection-statement
iteration-statement
jump-statement
(3.6.1) labeled-statement:
identifier : statement
case constant-expression : statement
default : statement
In the first of those choices of 3.6.1, the identifier is a label.
Does a label have a side-effect? If not, then whether the first form
of labeled statement has a side effect depends upon whether the
statement on the right hand side -always- has a side effect, a point
which remains to be proved at this point in the analysis.
In the second of those choices of 3.6.1, does a constant-expression
have a side effect? If not, then whether the second form of labeled
statement has a side effect depends upon whether the statement
on the right hand side -always- has a side effect, a point which
remains to be proved at this point in the analysis.
In the third of these choices of 3.6.1, does the presence of 'default'
have a side effect? If not then etc..
(3.6.2) compound-statement:
{declaration-list$opt$ statement-list$opt$}
Please excuse my typographic mangling of the subscript 'opt' to $opt$.
Does the -absence- of a declaration-list always have a side-effect
in a compound-statement? If not then determining whether compound-statements
always have side-effects, reduces down to determining whether
{statement-list$opt$} always has a side-effect. In such a statement,
does the -absence- of the statement-list always have a side-effect?
That is, is there a side-effect associated with the compound-statement
{}
?? If not then we have identified a compound-statement with no side-
effect, and have thus identified a statement with no side-effect,
which in turn would allow us to substitute in {} for the 'statement'
portion in all three choices of labeled-statement, so unless there
is a side-effect associated with the presence of a label or
constant-expression or 'default' label, labeled-statements need not
have any side-effects.
Similarily, expression-statement (3.6.3) is expression$opt$
and by simply exercising our option to not put in an expression,
we appear to have found another form of statement that has no side-effects.
If we examine section 3.6.3 of C89 properr, we find that it is entitled
3.6.3 Expression and Null Statements
and contains a paragraph:
A null statement (consisting of just a semicolon) performs no operations.
Personally, I find it difficult to see how there could be a "side-effect"
associated with a statement that the C89 standard *specifically*
says "performs no operations", but perhaps I'm just funny that way.
We can continue this analysis through selection-statement and
interation-statement and find several forms of each of those which have
no side-effects.
This brings us to (3.6.6) jump-statement
goto identifier ;
continue ;
break ;
return expression$opt$ ;
At this point there is room for disagreement over what constitutes a
'side-effect'. If a 'continue;' appears as the terminal statement
inside a loop, then has it had a side-effect or not, considering the
action would be the same if it was not there. If a 'break' appears as
the terminal statement of a loop which does not have post-statements
with side-effects (e.g., the third part of the for() is empty or has no
side effects) and which would not have executed any more interations
anyhow, then is it a side-effect? If a goto statement branches to the
next line, has it had a side-effect? If a return statement with no
expression appears as the last line of a void function, then
does that return have a side-effect?
I would be prepared to accept for the purposes of discussion
a definition that indicated that a jump-statement always has a
side-effect. But jump-statement is only one of the seven forms
of C89 statements and the other six all have valid (and sometimes useful)
forms that have no side-effects.
I must therefore disagree with the claim that,
all statements have side-
effects.
as there are numerous C statements possible that have no side-
effects.