Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 6th, 2006, 10:51 AM   #1
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
(void) before function call

I've seen something like:
(void)fprintf(stderr,"%s\n",strerror(errno) );
a few times.
Can't understand the purpose of (void) here?
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old May 6th, 2006, 10:59 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
fprintf returns the number of characters printed or a negative number on error. I would consider casting it to a void to be the act of an obsessive/compulsive. Just personal opinion, of course.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 6th, 2006, 12:18 PM   #3
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Jim seems to like it.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old May 6th, 2006, 1:19 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Again, my incipient blindness lead me to miss the semicolon and see that it was a declaration, rather than an invocation. The type of the return is, to me, essential, even though it isn't required in C. To use it in an invocation is another matter, entirely. If you don't specify the return type in 'C', then it's presumed to be an int, a condition which may not suit your purposes.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 6th, 2006, 2:29 PM   #5
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
(void) casts on fprintf error calls are an old deal - if you're in an error condition you don't check for more errors, you want to complain and die.

In production code I lint the heck out of everything. See what this says about your code:

http://www.splint.org/download.html
jim mcnamara is offline   Reply With Quote
Old May 6th, 2006, 9:02 PM   #6
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
Originally Posted by jim mcnamara
if you're in an error condition you don't check for more errors, you want to complain and die.
In that case can't we simply throw away the returned value. Why cast it to void?
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old May 6th, 2006, 10:15 PM   #7
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
It is an approach encouraged in some style guides as part of an overall strategy of ensuring that all data returned by a function is either explicitly used or deliberately discarded. Casting a return value to (void) is a way of explicitly discarding it, whereas simply calling the function and not looking at the return value is viewed as one way to accidentally discard data.

Some older C compilers (in what some claim was a fit of obsession by their developers, but was probably an attempt to support such style guides) complained if the return value of a function was not stored in a variable or tested in some way. The way to shut such compilers up (so meaningful error messages or warnings could be seen) was to cast the result of a function call to void if the return value was not going to be used.
grumpy is offline   Reply With Quote
Old May 7th, 2006, 12:13 AM   #8
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
Quote:
Originally Posted by InfoGeek
In that case can't we simply throw away the returned value. Why cast it to void?
Grumpy answered that - essentially to get past code standards.
Or more correctly - explicity state that we want to disregard the return code.
jim mcnamara is offline   Reply With Quote
Old May 7th, 2006, 4:07 AM   #9
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,193
Rep Power: 5 lectricpharaoh will become famous soon enough
Wow, I learn something new every day.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Old May 7th, 2006, 8:58 AM   #10
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Thanks guys.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC