|
Hi,
I want to write a program, where until we give the value of j as
non zero, it should go on asking us values of i and j, displaying the
message "Division by zero: Floating point exception" for every input
of j as zero. Once we give the value of both i and j as non-zero, it
displays i/j and terminates.
This is the sample code i tried:
#include <signal.h>
void sig_s(int signo);
void Divide();
main( )
{
signal(SIGFPE,sig_s);
Divide();
}
void Divide()
{
int i,j,k;
printf("Enter a number\n");
scanf("%d",&i);
printf("another number\n");
scanf("%d",&j);
if( (i==0) && (j==0))
{
printf("Total is %d\n",0);
exit(0);
}
else
{
printf("Total is %d\n",i/j);
}
}
void sig_s(int signo)
{
signal(SIGFPE,sig_s);
printf("Division by zero:Floating point exception\n");
Divide();
}
It works fine as you give the denominator as zero and ask for the new
number but if you give the valid values also (e.g. i=10, j=5) Then
also it went into the Signal handler function and call the signal
handler recursively.
Could you please suggest me what wrong i am doing here ???
Thanks,
Amit | |
Share:
|
"Amit Sharma" <sh*******@gmail.com> wrote: "Division by zero: Floating point
exception" for every input of j as zero. Once we give the value of both i and j as non-zero, ....
snipped
You can divide zero among as many persons who want a piece of it. Dividing
by zero is quite a different thing. MPJ | | |
>> "Division by zero: Floating point exception" for every input of j as zero. Once we give the value of both i and j as non-zero, ....
snipped You can divide zero among as many persons who want a piece of it. Dividing by zero is quite a different thing. MPJ
<OT>
True. However, it is better to help the OP _and_ correct his use of
the language than only help him.
Everything else is off-topic... and it is a nice gesture to mark it so.
As you said elsethread that you were new to the usenet I thought
mentioning that might be helpful.
It is tempting to answer when you know to say something but it
is more satisfactory when you actually can help the people. For
everyone.
</OT>
--Michael | | | MPJ wrote You can divide zero among as many persons who want a piece of it.
Dividing by zero is quite a different thing. MPJ "Michael Mair" wrote <OT> True. However, it is better to help the OP _and_ correct his use of the language than only help him. Everything else is off-topic... and it is a nice gesture to mark it so.
As you said elsethread that you were new to the usenet I thought mentioning that might be helpful. It is tempting to answer when you know to say something but it is more satisfactory when you actually can help the people. For everyone. </OT>
--Michael
I would be less than thrilled to see a perfectly-valid, ANSI C program
announce to me that 0/7 were division by zero. The language question I
didn't address at all, because I'm not sure if the fella is a ring theorist
who mistyped or a kid who needs to crack open his eighth-grade math book
before thinking of coding. MPJ
P.S. I like the word "elsethread." | | |
Hi there,
I will first comment on your code which probably will not make
you happy but I have some "useful" answer below. #include <signal.h>
#include <stdio.h> /* for printf and scanf */
#include <stdlib.> /* for exit */ void sig_s(int signo); void Divide(); main( )
That is either int main (), int main (void) or
int main (int argc, char *argv[]). { signal(SIGFPE,sig_s); Divide();
return 0; } void Divide() { int i,j,k;
k is unused printf("Enter a number\n"); scanf("%d",&i); printf("another number\n"); scanf("%d",&j); if( (i==0) && (j==0)) { printf("Total is %d\n",0); exit(0); } else { printf("Total is %d\n",i/j); } }
void sig_s(int signo) { signal(SIGFPE,sig_s); printf("Division by zero:Floating point exception\n"); Divide(); }
Please provide code samples that compile.
It works fine as you give the denominator as zero and ask for the new number but if you give the valid values also (e.g. i=10, j=5) Then also it went into the Signal handler function and call the signal handler recursively. Could you please suggest me what wrong i am doing here ???
The thread http://groups.google.de/groups?threa...s.uwaterloo.ca
(one URL line)
will give you enough answers. Dave Vandervies explains the whole
thing rather nicely.
In short: The signal does not go away after you called the signal
handler. Usually, the default signal handler is reinstated (which
does not happen as you once again put your own handler first).
So, there is no way that the program resumes normal operation
whithout you doing non-standard-C things (ask in your OS's
programmer newsgroup).
You can use standard conforming signal handlers for basic
cleanup or hasty output, but it is a Bad Idea to try to get
back into normal program flow.
This is only an abridged and sloppy explanation, so please read
the thread I refer to.
Cheers
Michael | | |
> I would be less than thrilled to see a perfectly-valid, ANSI C program announce to me that 0/7 were division by zero.
The program suggested otherwise...
The language question I didn't address at all, because I'm not sure if the fella is a ring theorist who mistyped or a kid who needs to crack open his eighth-grade math book before thinking of coding. MPJ
Hmmm, maybe a little bit more of your reasoning written in your
answer might have made your motives clearer.
P.S. I like the word "elsethread."
So do I - nice of you to provide me with an opportunity to use it ;-)
Cheers
Michael | | |
Thanks for the reply,
I went through the thread and found the possible
undefined behaviours by signal, Here is the solution i found seems to
be perfect using sigsetjmp & siglongjmp.
#include <signal.h>
#include <stdio.h>
#include <setjmp.h>
void sig_s(int signo);
jmp_buf buf;
main( )
{
int i,j,k;
signal(SIGFPE,SIG_IGN);
sigsetjmp(buf,1);
signal(SIGFPE,sig_s);
printf("Enter a number\n"); scanf("%d",&i);
printf("another number\n"); scanf("%d",&j);
printf("Total is %d",i/j);
}
void sig_s(int signo)
{
printf("Division by zero:Floating point exception\n");
siglongjmp(buf,1);
}
Thanks,
Amit
Michael Mair <ma********************@ians.uni-stuttgart.de> wrote in message news:<cj**********@infosun2.rus.uni-stuttgart.de>... Hi there,
I will first comment on your code which probably will not make you happy but I have some "useful" answer below.
#include <signal.h> #include <stdio.h> /* for printf and scanf */ #include <stdlib.> /* for exit */ void sig_s(int signo); void Divide(); main( ) That is either int main (), int main (void) or int main (int argc, char *argv[]). { signal(SIGFPE,sig_s); Divide(); return 0; } void Divide() { int i,j,k; k is unused printf("Enter a number\n"); scanf("%d",&i); printf("another number\n"); scanf("%d",&j); if( (i==0) && (j==0)) { printf("Total is %d\n",0); exit(0); } else { printf("Total is %d\n",i/j); } }
void sig_s(int signo) { signal(SIGFPE,sig_s); printf("Division by zero:Floating point exception\n"); Divide(); }
Please provide code samples that compile.
It works fine as you give the denominator as zero and ask for the new number but if you give the valid values also (e.g. i=10, j=5) Then also it went into the Signal handler function and call the signal handler recursively. Could you please suggest me what wrong i am doing here ???
The thread http://groups.google.de/groups?threa...s.uwaterloo.ca (one URL line) will give you enough answers. Dave Vandervies explains the whole thing rather nicely.
In short: The signal does not go away after you called the signal handler. Usually, the default signal handler is reinstated (which does not happen as you once again put your own handler first). So, there is no way that the program resumes normal operation whithout you doing non-standard-C things (ask in your OS's programmer newsgroup). You can use standard conforming signal handlers for basic cleanup or hasty output, but it is a Bad Idea to try to get back into normal program flow. This is only an abridged and sloppy explanation, so please read the thread I refer to.
Cheers Michael | | |
"Amit Sharma" wrote Thanks for the reply, I went through the thread and found the possible undefined behaviours by signal, Here is the solution i found seems to be perfect using sigsetjmp & siglongjmp.
#include <signal.h> #include <stdio.h> #include <setjmp.h>
void sig_s(int signo); jmp_buf buf;
main( ) {
int i,j,k; signal(SIGFPE,SIG_IGN); sigsetjmp(buf,1); signal(SIGFPE,sig_s); printf("Enter a number\n"); scanf("%d",&i); printf("another number\n"); scanf("%d",&j); printf("Total is %d",i/j); }
void sig_s(int signo) { printf("Division by zero:Floating point exception\n"); siglongjmp(buf,1); }
Thanks, Amit
And what did your compiler and linker tell you about this? Michael Mair wrote I will first comment on your code which probably will not make you happy but I have some "useful" answer below.
#include <signal.h> #include <stdio.h> /* for printf and scanf */ #include <stdlib.> /* for exit */ void sig_s(int signo); void Divide(); main( ) That is either int main (), int main (void) or int main (int argc, char *argv[]). { signal(SIGFPE,sig_s); Divide(); return 0; } void Divide() { int i,j,k; k is unused
Amit thinks it's perfect. Apparently his compiler has a -perfect switch. printf("Enter a number\n"); scanf("%d",&i); printf("another number\n"); scanf("%d",&j); if( (i==0) && (j==0))
Michael, wir haben uns gewundert ob er zerstreuter Professor oder eines
Schulbuches noetig sei. Er ist der Letzere. SIE, aber, waren halb blau als
Sie dachten dass null durch null unter Algebraisten definiert ist.
{ printf("Total is %d\n",0); exit(0); } else { printf("Total is %d\n",i/j); } }
void sig_s(int signo) { signal(SIGFPE,sig_s); printf("Division by zero:Floating point exception\n"); Divide(); }
Please provide code samples that compile.
It works fine as you give the denominator as zero and ask for the new number but if you give the valid values also (e.g. i=10, j=5) Then also it went into the Signal handler function and call the signal handler recursively. Could you please suggest me what wrong i am doing here ???
The thread http://groups.google.de/groups?threa...s.uwaterloo.ca (one URL line) will give you enough answers. Dave Vandervies explains the whole thing rather nicely.
In short: The signal does not go away after you called the signal handler. Usually, the default signal handler is reinstated (which does not happen as you once again put your own handler first). So, there is no way that the program resumes normal operation whithout you doing non-standard-C things (ask in your OS's programmer newsgroup). You can use standard conforming signal handlers for basic cleanup or hasty output, but it is a Bad Idea to try to get back into normal program flow. This is only an abridged and sloppy explanation, so please read the thread I refer to.
Cheers Michael
I have enough questions about signal.h and setjmp.h to start a new thread.
MPJ | | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
2 posts
views
Thread by Gary Robinson |
last post: by
|
2 posts
views
Thread by lpw |
last post: by
|
3 posts
views
Thread by Martin McCormick |
last post: by
|
4 posts
views
Thread by Eric Boutin |
last post: by
|
11 posts
views
Thread by Jackie |
last post: by
|
4 posts
views
Thread by manochavishal |
last post: by
|
18 posts
views
Thread by Sven |
last post: by
|
5 posts
views
Thread by david |
last post: by
|
9 posts
views
Thread by thagor2008 |
last post: by
| | | | | | | | | | |