-
signal handler
Hello
I am starting to understand signal processing in UNIX. I still have some doubts ...
In the following program (not written by me, of course) why is sigint_handler() declared inside main() ?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
int main(void)
{
void sigint_handler(int sig); /* prototype */
char s[200];
/* set up the handler */
if (signal(SIGINT, sigint_handler) == SIG_ERR) {
perror("signal");
exit(1);
}
printf("Enter a string:\n");
if (gets(s) == NULL)
perror("gets");
else
printf("You entered: \"%s\"\n", s);
return 0;
}
/* this is the handler */
void sigint_handler(int sig)
{
printf("Not this time!\n");
}
By the way, could someone give some easy to understand example of the use of the pointer returned by signal ().
Thanks in advance !
-
It is being prototyped because:
a) It's not declared in an included header.
b) It is included, but they just feel like prototyping it.
c) It is prototyped inside the function so the function can use it, and the actual prototyped function is or will be declared later.
In this case, since 'main' uses this function before it's been declared, they're prototyping it. They could have prototyped it outside of the function. Perhaps they want it only available to the main function. Example:
main { ... }
fun1{ ... }
fun2{ ... }
fun3{ ... }
If we don't prototype anything, and locally prototyep 'fun3' in 'main', then only 'main' will be able to use 'fun3', where as, if you had prototyped 'fun3' at the top of the file, all funcitons in the file could call it.
Quzah.