I know this doesn't specifically pertain to the scope of comp.lang.c,
but I don't know of a better forum to post it on, besides, what is the
practicality of comp.lang.c if it weren't for answers to questions
such as this? :)
I see an awful lot of code on a day to day basis like so:
(void) printf(...);
I was told in my fledgeling years that this was used as a hint to the
compiler to tell it not to save the return value of the called
function, because it isn't used. I always wondered about that,
considering the statement is not an assignment expression, and the
return value 'flows off the end of the expression' anyway.
Is this a useful modern optimisation? I would have thought not, but I
still see it in modern code. Surely a modern (or even ancient)
compiler can see that the result of printf() is not used, and so it
should perform any possible optimisations based on the fact the return
value isn't used.
Am I wrong? Thanks,
David. 8 1787
David M. Wilson wrote: I know this doesn't specifically pertain to the scope of comp.lang.c, but I don't know of a better forum to post it on, besides, what is the practicality of comp.lang.c if it weren't for answers to questions such as this? :)
I see an awful lot of code on a day to day basis like so:
(void) printf(...);
I was told in my fledgeling years that this was used as a hint to the compiler to tell it not to save the return value of the called function, because it isn't used. I always wondered about that, considering the statement is not an assignment expression, and the return value 'flows off the end of the expression' anyway.
Is this a useful modern optimisation? I would have thought not, but I still see it in modern code. Surely a modern (or even ancient) compiler can see that the result of printf() is not used, and so it should perform any possible optimisations based on the fact the return value isn't used.
Am I wrong? Thanks,
The compiler can see that the return code isn't used, but it can't tell
whether or not it SHOULD have been used. By the author specifying the
"void" cast, they're telling the compiler (or lint) not to issue a
warning for that line as the return code is, in this case, being
intentionally discarded.
Ed. David.
David M. Wilson wrote: I know this doesn't specifically pertain to the scope of comp.lang.c, but I don't know of a better forum to post it on, besides, what is the practicality of comp.lang.c if it weren't for answers to questions such as this? :)
I see an awful lot of code on a day to day basis like so:
(void) printf(...);
I was told in my fledgeling years that this was used as a hint to the compiler to tell it not to save the return value of the called function, because it isn't used.
I don't believe it accomplishes anything like that. The compiler
probably can't do anything useful with the information that the return
value is unused - the code for printf() (which probably resides in some
library somewhere that the compiler doesn't know or care about) is
already returning a value. printf isn't going to be re-written to omit
the return just because you cast it to void.
To the best of my knowledge, the cast-to-void technique is used solely
to silence warnings from static code checking tools like LINT, or from
over-zealous compilers.
I always wondered about that, considering the statement is not an assignment expression, and the return value 'flows off the end of the expression' anyway.
Is this a useful modern optimisation?
I don't believe it is, or has ever been, an optimization, useful or
otherwise.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
"David M. Wilson" wrote:
.... snip ... I see an awful lot of code on a day to day basis like so:
(void) printf(...);
I was told in my fledgeling years that this was used as a hint to the compiler to tell it not to save the return value of the called function, because it isn't used. I always wondered about that, considering the statement is not an assignment expression, and the return value 'flows off the end of the expression' anyway.
Opinions vary, but one attitude is that:
a) This avoids generating 'result discarded' messages in lint.
b) This indicates to any future maintainer that there IS a
result being returned and discarded, which he may use if the
occasion arises.
Another attitude is that it is meaningless clutter.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
On Tue, 6 Jan 2004, David M. Wilson wrote: I know this doesn't specifically pertain to the scope of comp.lang.c, but I don't know of a better forum to post it on, besides, what is the practicality of comp.lang.c if it weren't for answers to questions such as this? :)
I see an awful lot of code on a day to day basis like so:
(void) printf(...);
Hee hee!
Message-ID: <Pi**********************************@unix41.andre w.cmu.edu>
<quote> Ben.c:17:7: Return value (type int) ignored: putchar(p[i]) Result returned by function call is not used.
Gosh. Lint really _is_ prehistoric, isn't it?
According to K&R2, putchar() does indeed return an int. I suspect a cast to void would silence lint, but it's hardly worth it, is it?
Some old code, and some overly-portable code, does use casts to
void. We even get questions about (void)printf here occasionally.
</quote>
-Arthur
In article <99*************************@posting.google.com> , dw***********@botanicus.net (David M. Wilson) wrote: I know this doesn't specifically pertain to the scope of comp.lang.c, but I don't know of a better forum to post it on, besides, what is the practicality of comp.lang.c if it weren't for answers to questions such as this? :)
I see an awful lot of code on a day to day basis like so:
(void) printf(...);
I was told in my fledgeling years that this was used as a hint to the compiler to tell it not to save the return value of the called function, because it isn't used. I always wondered about that, considering the statement is not an assignment expression, and the return value 'flows off the end of the expression' anyway.
Is this a useful modern optimisation? I would have thought not, but I still see it in modern code. Surely a modern (or even ancient) compiler can see that the result of printf() is not used, and so it should perform any possible optimisations based on the fact the return value isn't used.
If a function returns a value, and the caller doesn't use that return
value, then there are two possibilities:
1. The caller made a mistake and should have used the value.
2. The caller knew what he/she was doing and had no need for the
value.
When you cast the return value to void, you explicitely tell the
compiler that yes, you know that the function returns a value, and no,
you don't want to use it. Without that cast, many compilers will give a
warning telling you that you might have missed something.
It is also a good hint to other programmers.
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi**********************************@unix45. andrew.cmu.edu>... On Tue, 6 Jan 2004, David M. Wilson wrote: I know this doesn't specifically pertain to the scope of comp.lang.c, but I don't know of a better forum to post it on, besides, what is the practicality of comp.lang.c if it weren't for answers to questions such as this? :)
I see an awful lot of code on a day to day basis like so:
(void) printf(...); Hee hee!
Message-ID: <Pi**********************************@unix41.andre w.cmu.edu> <quote>
>Ben.c:17:7: Return value (type int) ignored: putchar(p[i]) > Result returned by function call is not used.
Gosh. Lint really _is_ prehistoric, isn't it?
According to K&R2, putchar() does indeed return an int. I suspect a cast to void would silence lint, but it's hardly worth it, is it?
Some old code, and some overly-portable code, does use casts to void.
To my knowledge, I've never read anything to suggest it was being done
for portability reasons. Is there any evidence of such?
I believe the standard inherited (and it remains in C99) the K&R C
behaviour of allowing functions which don't have an explicit return
statement to function as normal, so long as the calling function
didn't attempt to use a value from that function. So, C has always had
issues with ignored function return values.
I can understand why lint (et al) would give the warning, e.g...
sin(x);
But, I can't see that actual programming instances where the warning
might actually highlight an error would be terribly common.
I'm all ears (well eyes), if anyone has any anecdotes.
We even get questions about (void)printf here occasionally.
</quote>
--
Peter
[..] I can understand why lint (et al) would give the warning, e.g...
What does "et al" mean?
--
Vijay Kumar R Zanvar
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> writes: [..] I can understand why lint (et al) would give the warning, e.g...
What does "et al" mean?
"and others"
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Trevor Lango |
last post by:
I want to be able to cast away the constness of a private member variable in
a member function of a class.
I have the private data member declared as follows:
const double x;
I have an...
|
by: Ramesh Tharma |
last post by:
Hi,
Is any one knows what's wrong with the following code, I was told that it
will compile and run but it will crash for some values.
Assume that variables are initilized.
char* c;
long*...
|
by: mr_semantics |
last post by:
I have been reading about the practise of casting values to unsigned
char while using the <ctype.h> functions. For example,
c = toupper ((unsigned char) c);
Now I understand that the standard...
|
by: Tigger |
last post by:
I have an object which could be compared to a DataTable/List which I am
trying to genericify.
I've spent about a day so far in refactoring and in the process gone
through some hoops and hit some...
|
by: S. Lorétan |
last post by:
Hi guys,
Sorry for this stupid question, but I don't know why it isn't working.
Here is my (example) code:
namespace Test {
class A {
public string Label1;
}
|
by: mijobee |
last post by:
Hello Everyone,
I just wanted to check that I'm using dynamic_cast correctly. I have a
hierarchy of objects using pure virtual classes and virtual inheritance
to implement interfaces. I ran...
|
by: Alex Vinokur |
last post by:
classes that have virtual methods hold pointer to virtual table as
additional implicit data member. So, sizeof of such classes is sizeof
of all data (as struct-POD) plus 4.
It seems that use of...
|
by: Ajeet |
last post by:
hi
I am having some difficulty in casting using generics. These are the
classes.
public interface IProvider<PROF>
where PROF : IProviderProfile
{
//Some properties/methods
}
|
by: Phil Latio |
last post by:
I have a number of spreadsheets, each with between 1000-6000 rows (each row
is a property) and they all need to be combined into a single database. Each
spreadsheet contains slightly different...
|
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= |
last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting
more often than creating an instance of objects.
like :
protected void gvOrderDetailsRowDataBound(object sender,...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |