473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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 .
Nov 14 '05 #1
3 5650

"Jason luo" <ka***@eastnet.cn> wrote in message
news:8f**************************@posting.google.c 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.

Nov 14 '05 #2
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

Nov 14 '05 #3
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
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
2
by: dam_fool_2003 | last post by:
Friends, I come across a resent thread describes the differences between getc() vs fgetc(). There some body stated the sentence "evaluate its parameter more than once". I cannot understand the...
3
by: sandy | last post by:
I cannot figure this out. I have the following code: <code> Private: /****************************************************************************** CreateList
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
56
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.