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

Discussion in 'C Programming' started by Jason luo, Aug 18, 2004.

  1. Jason luo

    Jason luo Guest

    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 .
    Jason luo, Aug 18, 2004
    #1
    1. Advertising

  2. "Jason luo" <> wrote in message
    news:...
    > 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.
    William L. Bahn, Aug 18, 2004
    #2
    1. Advertising

  3. Jason luo

    Michael Mair Guest

    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
    Michael Mair, Aug 18, 2004
    #3
  4. Jason luo

    Peter Ammon Guest

    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
    Peter Ammon, Aug 19, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian
    Replies:
    0
    Views:
    1,800
  2. renata
    Replies:
    1
    Views:
    777
    Kevin Spencer
    Jun 25, 2003
  3. suzy
    Replies:
    3
    Views:
    433
  4. Daniel Walzenbach
    Replies:
    5
    Views:
    402
    =?Utf-8?B?RGFuaWVsIFdhbHplbmJhY2g=?=
    Feb 3, 2004
  5. =?Utf-8?B?bWFrdGhhcg==?=
    Replies:
    2
    Views:
    317
    =?Utf-8?B?bWFrdGhhcg==?=
    Feb 27, 2004
Loading...

Share This Page