Reputation: 1503
With respect to the return statement, the Microsoft Visual Studio documentation says
Syntax:
return expression;
where expression is marked as optional. It continues
The value of expression, if present, is returned to the calling function. If expression is omitted, the return value of the function is undefined.
This is quite clear, but on the other hand, there is the notion of an empty expression. This makes me confuse. Thinking of the empty expression not as nothing, but as an expression which is empty, I would think that if we have a function
void foo(void)
{
return;
}
then the expression foo() could be used wherever the empty expression is allowed. For example, the behaviour of the code
unsigned int i=0;
for(foo();i<10;i++) printf("%u",i);
would be defined. I am aware that this is probably of little practical relevance, but I would like to understand why, in this context, the empty expression is not an expression.
Upvotes: 1
Views: 123
Reputation: 122383
It's called void expression. And you can use a void expression in for
like you did. The void expression does nothing but its side effect, which is calling the function.
In fact, if the first clause of for
is any type of expression, it's evaluated as a void expression:
C99 6.8.5.3 The
for
statement... If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.
and
C99 6.3.2.2
void
The (nonexistent) value of a void expression(an expression that has type
void
) shall not be used in any way, and implicit or explicit conversions (except tovoid
) shall not be applied to such an expression. If an expression of any other type is evaluated as a void expression, its value or designator is discarded. (A void expression is evaluated for its side effects.)
Upvotes: 4
Reputation:
I don't know if there actually is such a thing in C as "the empty expression", but if there is, then you are confusing it with the type void
, and at the same time you are confusing the act of returning "nothing" from a non-void
function (which is illegal) with leaving out the initializer expression of a for
loop (which is legal).
Upvotes: 1