Velocity Reviews > How to explain "evaluate the expression as a void expression"?

# How to explain "evaluate the expression as a void expression"?

Jason luo
Guest
Posts: n/a

 08-18-2004
Hi all,

In c99-standard page 52,there is a sentence about void,as below:

If an expression of any other type is evaluated as a void expression,
its value or designator is discarded.

I don't know how to understand it, How to evaluate the expression as a
void expression? explicit conversion the expression? but, befor the
sentence ,"implicit or explicit conversions (except to void) shall not
be applied to such an expression. " was mentioned.

thanks .

William L. Bahn
Guest
Posts: n/a

 08-18-2004

"Jason luo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi all,
>
> In c99-standard page 52,there is a sentence about void,as

below:
>
> If an expression of any other type is evaluated as a void

expression,
> its value or designator is discarded.
>
> I don't know how to understand it, How to evaluate the

expression as a
> void expression? explicit conversion the expression? but, befor

the
> sentence ,"implicit or explicit conversions (except to void)

shall not
> be applied to such an expression. " was mentioned.
>
> thanks .

This is the way I read that part:

It starts off by talking about expressions that are of type void.
For instance, an expression that calls a void function would be a
void expression. If foo(k) is a void function, it's saying that
you can't do something like:

k = foo(k); (implicit conversion)
k = (int) foo(k); (explicit conversion)

But you can call:

(void) foo(k);

all day because you are allowed to cast a void expression to
void.

Nor could you do something like:

k = (int) ( (void) i++ );

Since you took an expression having a type and cast it to void.
It is now a void expression and you can't get at the value again
by doing a subsequent cast on it. As soon as it because a void
expression, the value became indeterminate and/or nonexistent.

You use void expressions because you want the side effect. The
only reason I can think of to cast an expression that has a type
to void is to perhaps suppress a compiler warning about unused
code or code having no effect or something like that. With the
operators that produce side effects I don't think you would get
such a warning - the compiler knows about the side effects of
those operators. I don't know if you it might be useful when
working with volatile variables.

Michael Mair
Guest
Posts: n/a

 08-18-2004
Hiho,

> You use void expressions because you want the side effect. The
> only reason I can think of to cast an expression that has a type
> to void is to perhaps suppress a compiler warning about unused
> code or code having no effect or something like that. With the
> operators that produce side effects I don't think you would get
> such a warning - the compiler knows about the side effects of
> those operators. I don't know if you it might be useful when
> working with volatile variables.

In a similar vein, you can use this to tell lint-like programs
that you actually are aware of the implications of what you
are doing, getting rid of a warning.
A nice example are the scanf() family functions. Many people
just ignore the return value and do not check ist.
splint will tell you about the unused return value, but if
you do something like

(void) scanf(....);

you make clear that you did not just forget to do

if( (num=scanf(....)) != INTENDED_NUM ) {
/* Handle scanf error */
....
}

I am not sure whether this is good or bad style as I rather
go for the return value, but it has the advantage that you
yourself also know that you are discarding the respective
information willingly and wittingly.

Cheers
Michael

Peter Ammon
Guest
Posts: n/a

 08-19-2004
Jason luo wrote:
> Hi all,
>
> In c99-standard page 52,there is a sentence about void,as below:
>
> If an expression of any other type is evaluated as a void expression,
> its value or designator is discarded.
>
> I don't know how to understand it, How to evaluate the expression as a
> void expression? explicit conversion the expression?

That's one way, e.g. (void)printf("Hello World")

There are other ways. For example, from 6.8.3,

"The expression in an expression statement is evaluated as a void
expression"

So in a statement like

i++;

the i++ is evaluated as a void expression, meaning its value is
discarded. And, of course, the result of functions returning type void
is a void expression.

> but, befor the
> sentence ,"implicit or explicit conversions (except to void) shall not
> be applied to such an expression. " was mentioned.
>
> thanks .

So stuff like (int)(void)5 is illegal.

HTH

-Peter