Results 1 to 7 of 7
I found this snippet of code and I'm having a hard time understanding why its preceded by (void)...
Code:
(void)signal(SIGINT, SIG_DFL);
When I remove the (void) the functions appears to ...
- 11-15-2008 #1
[SOLVED] signals in C
I found this snippet of code and I'm having a hard time understanding why its preceded by (void)...
When I remove the (void) the functions appears to work properly...is there any reason for the the (void)?...Gerard4143Code:(void)signal(SIGINT, SIG_DFL);
Code:signal(SIGINT, SIG_DFL);
- 11-15-2008 #2
here is your answer Void type - Wikipedia, the free encyclopedia
- 11-15-2008 #3
Interesting article but it doesn't answer the question...Why would you have code
Since the (void) doesn't server any purpose that I can see and the function callCode:(void)signal(SIGINT, SIG_DFL);
produces the same result...Gerard4143Code:signal(SIGINT, SIG_DFL);
Ooops there it is at the end of the article...a throw back from earlier times that explains the casting to (void)...Thanks
- 11-15-2008 #4It's a good answer, and almost complete.a throw back from earlier times
Two comments:
- Some more modern products can also check whether you look at the value returned from a function call. If you call a particular function several times in your code, and check the result most of the time, (void) can be used to override the checking of the one call where (you hope!) it doesn't matter.
- Although it's difficult to accidentally get an error from signal(), as a matter of form it's a good idea to check the return value anyway. You may have coded correctly, but if someone comes along later and modifies the code incorrectly, wouldn't you want him to have some notification that this had happened? (Of course, this is a religious issue.)
--
Bill
Old age and treachery will overcome youth and skill.
- 11-15-2008 #5
Thank-you for the replies. I think in this case its a throw back because:
1. the function result is not compared to anything and
2. the function result is not saved.
Again thank-you for the replies...Grerard4143
- 11-16-2008 #6True, but that doesn't necessarily imply use of an ancient C compiler. I fear that I didn't make myself clear previously. Allow me to muddy the waters a little more. :)
- the function result is not compared to anything and
- the function result is not saved.
Modern compilers usually issue neither error indications nor warnings for statements like these:
Someone who wishes to write bulletproof code, though, will be interested in flagging many of these. He might even be interested in checking each printf() use for a negative returned value, which would indicate an error.Code:signal(SIGINT, SIG_DFL); printf("Hello, world!\n");
To enforce this, he might find and use a product similar to the old flex (no relation to the Adobe product), which may still be available on some commercial UNIX systems. Its purpose was to be hypercritical of C programs, looking for unusual (though legal) code which could contain bugs.
Or he can use a commercial product, like the one I linked to previously. I have used it and recommend it. ("They" are not paying me, and probably don't even know that I'm talking about their product.)
Someone can use such a product, instructing it to warn about any calling of a function without using the returned value. This is useful when writing bulletproof code, because it forces the user to confront each place where his code doesn't check for an error indication upon return from a function call.
It's perfectly reasonable for the user to do this, but still not be interested in error checking at certain points in his code. At such points, he will call functions as in this example:
Such a statement doesn't do anything different, but it does force the code checking software to avoid worrying about what is done with the returned value from the function call.Code:(void)signal(SIGINT, SIG_DFL);
Ok, then. So the above example will hint at the use of either an old C compiler, or a source code checking program.
I hope this is a teensy bit clearer. :)--
Bill
Old age and treachery will overcome youth and skill.
- 11-16-2008 #7
Now i see what your driving at Bill...your responses are always a learning experience...Thanks again Gerard4143
Just to be sure...your saying a programmer would use the void cast (as in my example) to explicitly state that the return value should be ignored...


