![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
(void) before function call
I've seen something like:
(void)fprintf(stderr,"%s\n",strerror(errno) ); Can't understand the purpose of (void) here?
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
(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 |
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
Quote:
Or more correctly - explicity state that we want to disregard the return code. |
|
|
|
|
|
|
#9 |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,193
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Thanks guys.
__________________
PFO - My daily dose of technology. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|