473,230 Members | 1,549 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,230 software developers and data experts.

question about void typecast

I was doing a little bit of reading in the ISO C spec. about
typecasting to a void type. This caused me to have a question. In
particular, I'm curious to know about section 6.3.2.2 where the specs
says "is evaluated as a void expression, its value or designator is
discarded."

If I do the following:

#include <stdio.h>

int
main(int argc, char **argv)
{
int s = 12;

printf("non-void type = 0x%x\n", s);

(void)s;

return(0);
}

What is the effect of typecasting s to a void type. Does the compiler
just treat the line as a null statement?

Thanks.

-brad walker
Dec 26 '07 #1
8 3605
On Wed, 26 Dec 2007 08:25:22 -0800, brad2000 wrote:
I was doing a little bit of reading in the ISO C spec. about typecasting
to a void type. This caused me to have a question. In particular, I'm
curious to know about section 6.3.2.2 where the specs says "is
evaluated as a void expression, its value or designator is discarded."

If I do the following:

(void)s;

What is the effect of typecasting s to a void type. Does the compiler
just treat the line as a null statement?
Yes, that's it. The expression s is evaluated, and then its value is
converted to void, so discarded. As a statement, you might as well have
written

s;

since this discards the value as well. Casts to void are useful pretty
much only for suppressing warnings. There is nothing wrong with

printf("Hello, world!\n");

but some tools will generate a warning that the return value of printf is
discarded, and require you to write

(void) printf("Hello, world!\n");

to indicate that you want to discard it. Similarly, some tools will
generate a warning for unused function parameters, and one use in a
conversion to void is enough to silence them. Whether that's a good idea
is mostly a style issue, and something you'll have to decide for
yourself. I don't believe there's any case in standard C where a
conversion to void is actually required.
Dec 26 '07 #2
br******@gmail.com wrote:
I was doing a little bit of reading in the ISO C spec. about
typecasting to a void type.
Just FYI, 'typecasting' is what happens to actors who play the same role
too long.
C has 'casting'. a cast is an explicit conversion from one type to another.
int s = 12;
(void)s;
What is the effect of typecasting s to a void type.
It causes s to be evaluated as a void type. Since the result isn't
assigned to anything, it is thrown away.
Does the compiler just treat the line as a null statement?
It was /already/ a null statement since it had no side-effects and
didn't assign anything! Any good compiler would entirely optimise it out.
Dec 26 '07 #3
On Wed, 26 Dec 2007 18:06:05 +0000, Mark McIntyre
<ma**********@spamcop.netwrote in comp.lang.c:
br******@gmail.com wrote:
I was doing a little bit of reading in the ISO C spec. about
typecasting to a void type.

Just FYI, 'typecasting' is what happens to actors who play the same role
too long.
C has 'casting'. a cast is an explicit conversion from one type to another.
Thanks for that, so I don't have to point it out.
int s = 12;
(void)s;
What is the effect of typecasting s to a void type.

It causes s to be evaluated as a void type. Since the result isn't
assigned to anything, it is thrown away.
I know you know what you mean here, but I think your wording came out
poorly in the first sentence of the paragraph above.

I would not say 's' is evaluated as a void type, since void is an
incomplete type and incomplete types cannot be evaluated.

In the abstract machine, 's' is evaluated by performing lvalue to
rvalue conversion, yielding the int value 12.

The (void) does not perform an actual case, since you cannot convert a
value, or anything else, to an incomplete type.

Instead, I think the best way to define the meaning of an apparent
cast to void is actually to state that it is an explicit statement (to
the compiler, to code analysis tools, people reading the code) that
the value of the expression is explicitly discarded.

There is really no such thing as a "cast to void", anymore than there
is "passing a void to a function".
Does the compiler just treat the line as a null statement?

It was /already/ a null statement since it had no side-effects and
didn't assign anything! Any good compiler would entirely optimise it out.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Dec 26 '07 #4
br******@gmail.com writes:
I was doing a little bit of reading in the ISO C spec. about
typecasting to a void type. This caused me to have a question. In
particular, I'm curious to know about section 6.3.2.2 where the specs
says "is evaluated as a void expression, its value or designator is
discarded."

If I do the following:

#include <stdio.h>

int
main(int argc, char **argv)
{
int s = 12;

printf("non-void type = 0x%x\n", s);

(void)s;

return(0);
}

What is the effect of typecasting s to a void type. Does the compiler
just treat the line as a null statement?
We usually refer to this as "casting" rather than "typecasting".

A cast is an operator, consisting of a type name in parentheses, that
specifies a type conversion. Type conversions can be either explicit
(specified by a cast operator) or implicit; there's no such thing as
an implicit cast. (I know you didn't mention "implicit casts", but
it's such a common error that I thought I'd take the opportunity to
clear it up in advance.)

In this particular context, the cast does nothing. This:
(void)s;
and this:
s;
do exactly the same thing: evaluate ``s'' and discard the result. In
the first statement, the result is discarded by the cast; in the
second, the result is discarded because it's an expression statement
(any expression with a semicolon appended to it can be an expression
statement).

Since the only thing evaluating ``s'' does is obtain its value, and
since that value is discarded anyway, the compiler can and almost
certainly will generate no code for the statement. (This wouldn't be
the case if s were declared volatile; don't worry about that.)

A couple of other comments about your code:

For printf, "%x" expects an unsigned int; you're giving it a signed
int. It happens that you can get away with it in this case, but it's
best to use the right format for the type:
printf("non-void type = %d\n", s);

The parentheses on the return statement are allowed, but they aren't
necessary:
return 0;

If you don't need command-line arguments, you can declare main as:
int main(void)
but it *must* return an int, whether you expect to use the result or
not. (That's not 100% true, but it's close enough.)

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 26 '07 #5
Jack Klein <ja*******@spamcop.netwrites:
On Wed, 26 Dec 2007 18:06:05 +0000, Mark McIntyre
<ma**********@spamcop.netwrote in comp.lang.c:
>br******@gmail.com wrote:
[...]
Thanks for that, so I don't have to point it out.
> int s = 12;
(void)s;
What is the effect of typecasting s to a void type.

It causes s to be evaluated as a void type. Since the result isn't
assigned to anything, it is thrown away.

I know you know what you mean here, but I think your wording came out
poorly in the first sentence of the paragraph above.

I would not say 's' is evaluated as a void type, since void is an
incomplete type and incomplete types cannot be evaluated.
Right, ``s'' is evaluted as an expression of type int; the result is
then converted to void.
In the abstract machine, 's' is evaluated by performing lvalue to
rvalue conversion, yielding the int value 12.

The (void) does not perform an actual case, since you cannot convert a
value, or anything else, to an incomplete type.

Instead, I think the best way to define the meaning of an apparent
cast to void is actually to state that it is an explicit statement (to
the compiler, to code analysis tools, people reading the code) that
the value of the expression is explicitly discarded.

There is really no such thing as a "cast to void", anymore than there
is "passing a void to a function".
That would be a valid and consistent way to describe it, but I'm
afraid the standard disagrees with your wording.

See C99's description of cast operators (6.5.4) and void conversions
(6.3.2.2).

Conversion to void discards the value.
Does the compiler just treat the line as a null statement?

It was /already/ a null statement since it had no side-effects and
didn't assign anything! Any good compiler would entirely optimise it out.
In the abstract machine, ``s'' is evaluated before the value is
discarded. An extremely naive compiler might actually load the value
of ``s'' into, say, a register. But yes, eliminating such an
evaluation is a common, if not universal, optimization.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 26 '07 #6
On Wed, 26 Dec 2007 14:07:56 -0800, Keith Thompson <ks***@mib.org>
wrote in comp.lang.c:
Jack Klein <ja*******@spamcop.netwrites:
On Wed, 26 Dec 2007 18:06:05 +0000, Mark McIntyre
<ma**********@spamcop.netwrote in comp.lang.c:
br******@gmail.com wrote:
[...]
Thanks for that, so I don't have to point it out.
int s = 12;
(void)s;
What is the effect of typecasting s to a void type.

It causes s to be evaluated as a void type. Since the result isn't
assigned to anything, it is thrown away.
I know you know what you mean here, but I think your wording came out
poorly in the first sentence of the paragraph above.

I would not say 's' is evaluated as a void type, since void is an
incomplete type and incomplete types cannot be evaluated.

Right, ``s'' is evaluted as an expression of type int; the result is
then converted to void.
Actually, the standard does not say that. In fact it disallows it:

6.2.5 p19 "The void type comprises an empty set of values; it is an
incomplete type that cannot be completed."

6.3 p1 "Several operators convert operand values from one type to
another automatically. This subclause specifies the result required
from such an implicit conversion, as well as those that result from a
cast operation (an explicit conversion)."

A conversion is something that converts values from one type to
another type. It is impossible to convert any value to the void type,
since it is defined as not having any values at all.
In the abstract machine, 's' is evaluated by performing lvalue to
rvalue conversion, yielding the int value 12.

The (void) does not perform an actual case, since you cannot convert a
value, or anything else, to an incomplete type.

Instead, I think the best way to define the meaning of an apparent
cast to void is actually to state that it is an explicit statement (to
the compiler, to code analysis tools, people reading the code) that
the value of the expression is explicitly discarded.

There is really no such thing as a "cast to void", anymore than there
is "passing a void to a function".

That would be a valid and consistent way to describe it, but I'm
afraid the standard disagrees with your wording.

See C99's description of cast operators (6.5.4) and void conversions
(6.3.2.2).

Conversion to void discards the value.
There is no such thing as conversion to void. It is not possible to
convert a value of any type to a void value. In fact, even though
6.3.2.2 is in the conversions section, it reiterates the fact that a
void expression (not void type) has a nonexistent value.

A (void) cast operator creates a void expression, it does not cause a
value to be converted to void.

It is not possible to convert a value to void.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Dec 27 '07 #7
Jack Klein <ja*******@spamcop.netwrites:
On Wed, 26 Dec 2007 14:07:56 -0800, Keith Thompson <ks***@mib.org>
wrote in comp.lang.c:
>Jack Klein <ja*******@spamcop.netwrites:
On Wed, 26 Dec 2007 18:06:05 +0000, Mark McIntyre
<ma**********@spamcop.netwrote in comp.lang.c:
br******@gmail.com wrote:
[...]
Thanks for that, so I don't have to point it out.

int s = 12;
(void)s;
What is the effect of typecasting s to a void type.

It causes s to be evaluated as a void type. Since the result isn't
assigned to anything, it is thrown away.

I know you know what you mean here, but I think your wording came out
poorly in the first sentence of the paragraph above.

I would not say 's' is evaluated as a void type, since void is an
incomplete type and incomplete types cannot be evaluated.

Right, ``s'' is evaluted as an expression of type int; the result is
then converted to void.

Actually, the standard does not say that. In fact it disallows it:

6.2.5 p19 "The void type comprises an empty set of values; it is an
incomplete type that cannot be completed."

6.3 p1 "Several operators convert operand values from one type to
another automatically. This subclause specifies the result required
from such an implicit conversion, as well as those that result from a
cast operation (an explicit conversion)."

A conversion is something that converts values from one type to
another type. It is impossible to convert any value to the void type,
since it is defined as not having any values at all.
[snip]
>See C99's description of cast operators (6.5.4) and void conversions
(6.3.2.2).

Conversion to void discards the value.

There is no such thing as conversion to void. It is not possible to
convert a value of any type to a void value. In fact, even though
6.3.2.2 is in the conversions section, it reiterates the fact that a
void expression (not void type) has a nonexistent value.

A (void) cast operator creates a void expression, it does not cause a
value to be converted to void.

It is not possible to convert a value to void.
Hmm. I don't have my copy of the standard handy at the moment. When
I read the section that describes the cast operator, I didn't see any
exception for ``(void)'' casts; as I recall, it said that a cast
converts the expression to the target type. I'll take a closer look
at both sections later and post again. Possibly the standard is
internally inconsistent.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 27 '07 #8
br******@gmail.com wrote:
I was doing a little bit of reading in the ISO C spec. about
typecasting to a void type. This caused me to have a question. In
particular, I'm curious to know about section 6.3.2.2 where the specs
says "is evaluated as a void expression, its value or designator is
discarded."

If I do the following:

#include <stdio.h>

int
main(int argc, char **argv)
{
int s = 12;

printf("non-void type = 0x%x\n", s);

(void)s;

return(0);
}

What is the effect of typecasting s to a void type. Does the compiler
just treat the line as a null statement?

Thanks.

-brad walker
This is *literally* a discussion about nothing!
Dec 27 '07 #9

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

Similar topics

1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
9
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin...
18
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for...
22
by: nick | last post by:
i do not know what is the use of (e.g. void *pt), when will use it. thanks!
12
by: chump1708 | last post by:
can anyone tell me what does the following mean?? 1. void* abc() { } is void a generic pointer??? and if yes what does it mean....
2
by: BKMiller | last post by:
Hello everyone, I'm just getting started playing around with C++, so please don't laugh too loudly at my crude source code, okay? (o^^o) I'm combining together several introductory exercises...
10
by: kkirtac | last post by:
Hi, i have a void pointer and i cast it to an appropriate known type before using. The types which i cast this void* to are, from the Intel's open source computer vision library. Here is my piece...
28
by: junky_fellow | last post by:
Guys, Consider a function func(void **var) { /* In function I need to typecast the variable var as (int **) I mean to say, I need to access var as (int **) }
10
by: Chris Becke | last post by:
In C and in older C++ compilers, you could write code like this void a(void** pp); // declare a function that takes a pointer to a pointer-to-void. struct I {}; // some struct. I *i1,**i2; //...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.