Results 1 to 8 of 8
Is SIGFPE signal automatically raised and sent when a division by 0 occurs(just for example).Because , I thought that it would as I wrote in the following program , but ...
- 04-04-2008 #1Just Joined!
- Join Date
- Mar 2008
- Posts
- 12
Signal - SIGFPE
Is SIGFPE signal automatically raised and sent when a division by 0 occurs(just for example).Because , I thought that it would as I wrote in the following program , but when I run the program , nothing happens ; my handler is not called :
when run , if I enter 2 0 for example , it just does nothing . No "Argggh.." .Code:void sigfpe_handl(int sig) { printf("Argggh..Division by zero !\n"); //..some more } void sigint_handl(int sig) { printf("Program is exiting.."); exit(0); } int main() { signal(SIGINT,sigint_handl); signal(SIGFPE,sigfpe_handl); float x,y; while(1) { scanf("%f %f",&a,&b); if(b) printf("%g",a/b); } return 0; }
So why ?
I just read a bit about signal in book , where there are examples of SIGINT and SIGALRM , and then I try the above program . Please help me . Thanks in advance .
- 04-05-2008 #2Just Joined!
- Join Date
- Mar 2008
- Posts
- 12
Any hint ?
- 04-05-2008 #3
try integers instead of floats
#include <stdio.h>
#include <signal.h>
void sigfpe_handl(int sig)
{
printf("Argggh..Division by zero !\n");
}
int main()
{
signal(SIGFPE,sigfpe_handl);
int x,y;
fscanf(stdin, "%d", &x);
fscanf(stdin, "%d", &y);
fprintf(stdout, "x->%d\n", x);
fprintf(stdout, "y->%d\n", y);
printf("%d",x/y);
fputs("\n",stdout);
return 0;
}
- 04-05-2008 #4
Oh, good grief!
Here's your problem. Take a look at the line I've highlighted in red in your program:
Your program is never going to divide by zero, now, is it?Code:void sigfpe_handl(int sig) { printf("Argggh..Division by zero !\n"); //..some more } void sigint_handl(int sig) { printf("Program is exiting.."); exit(0); } int main() { signal(SIGINT,sigint_handl); signal(SIGFPE,sigfpe_handl); float x,y; while(1) { scanf("%f %f",&a,&b); if(b) printf("%g",a/b); } return 0; }
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.
- 04-05-2008 #5Just Joined!
- Join Date
- Mar 2008
- Posts
- 12
Oh , in the 1st post , I miss this line :
. Sorry . Without this , of course , there would not be anthing to ask . But with this line , there's nothing happen , as I mentioned !Code:float temp = a/b
And if I change float to int , as gerard said , there are a lot of "Arrggh.." (infinite!) if I enter 0 for b
To summarize :
- With float , there's nothing
- With int , sigfpe_handl wil be repeated infinitely
- 04-05-2008 #6
Ok, this is progress.
But please, instead of posting English descriptions of changes to your code, could you please please please post the complete, latest-version code which has the problem? (The float version, of course, because the int one acts as you expect it to.)--
Bill
Old age and treachery will overcome youth and skill.
- 04-05-2008 #7Just Joined!
- Join Date
- Mar 2008
- Posts
- 12
Here it is for the floats :
And for the ints , it doesn't work as I expect also . Because , it should print only one error message - if there is error - and process next I/O , so on until Ctrl-C . But with one error , it prints many error messages - that's the problem !Code:void sigfpe_handl(int sig) { printf("Argggh..Division by zero !\n"); //..some more } void sigint_handl(int sig) { printf("Program is exiting.."); exit(0); } int main() { signal(SIGINT,sigint_handl); signal(SIGFPE,sigfpe_handl); float x,y,temp; while(1) { scanf("%f %f",&a,&b); temp = a / b ; if(b) printf("%g",a/b); } return 0; }
- 04-05-2008 #8
This can't be the complete program, because you don't declare a or b anywhere.
Please do this:
- Edit the program to the way you want it. THEN
- Compile it. If it doesn't compile, go back to the first step. If it does compile, THEN
- Run it. If it doesn't give the results you wish to discuss, go back to the first step. If it does, THEN
- Post the complete program, #includes and all.
Then let's take a look at it. :)--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote