Hi, I'm doing an assignment which throws an error when meeting division-by-zero error using Signals in C library signal.h and let user input another 2 numbers, if no error just continue letting user input 2 numbers and print out quotient of them. Here is my attempt:

#include <stdio.h>
#include <signal.h>

void sign_zeroError(int sig)
{
	printf("Error with 0") ;
}

int main()
{
	signal(SIGFPE , sign_zeroError) ;

	while(1)
	{
		int a , b , result;
		printf("Input a: ");
		scanf("%d" , &a) ;
		printf("Input b: ");
		scanf("%d" , &b) ;
		result = a/b ;
		
		printf("Result: %d\n" , result);
	}
	return 0 ;
}

The problem is that when getting the error signal, the program will go on a continuous loop and i don't know how to get back to the routine letting user input 2 numbers. I'm not used to using signal and examples in textbook don't seem to be enough, hope to hear from you guys soon, thanx a lot in advance.

use break; in the end of while loop to exit from the loop.

while(1)
	{
		int a , b , result;
		printf("Input a: ");
		scanf("%d" , &a) ;
		printf("Input b: ");
		scanf("%d" , &b) ;
		result = a/b ;
		
		printf("Result: %d\n" , result);
                                break;
	}

hey guys i found some problem while compling my code...it show error in signal.h header file.

log:
==
In file included from /usr/include/sys/signal.h:34,
from /usr/include/signal.h:26,
from signal.c:2:
/usr/include/sys/siginfo.h:259: error: parse error before "ctid_t"
/usr/include/sys/siginfo.h:292: error: parse error before '}' token
/usr/include/sys/siginfo.h:294: error: parse error before '}' token
/usr/include/sys/siginfo.h:390: error: parse error before "ctid_t"
/usr/include/sys/siginfo.h:392: error: conflicting types for `__proc'
/usr/include/sys/siginfo.h:261: error: previous declaration of `__proc'
/usr/include/sys/siginfo.h:398: error: conflicting types for `__fault'
/usr/include/sys/siginfo.h:267: error: previous declaration of `__fault'
/usr/include/sys/siginfo.h:404: error: conflicting types for `__file'
/usr/include/sys/siginfo.h:273: error: previous declaration of `__file'
/usr/include/sys/siginfo.h:420: error: conflicting types for `__prof'
/usr/include/sys/siginfo.h:287: error: previous declaration of `__prof'
/usr/include/sys/siginfo.h:424: error: conflicting types for `__rctl'
/usr/include/sys/siginfo.h:291: error: previous declaration of `__rctl'
/usr/include/sys/siginfo.h:426: error: parse error before '}' token
/usr/include/sys/siginfo.h:428: error: parse error before '}' token
/usr/include/sys/siginfo.h:432: error: parse error before "k_siginfo_t"
/usr/include/sys/siginfo.h:437: error: parse error before '}' token

Just to add information to this post ...i mentioned my effort.

oh, the problem is i can't break the loop, if i do, then the program will work like:

_ Accept a and b.
_ Print out a/b or error if found.

My program needs to work like:

_ Accept a and b.
_ Print out a/b or error if found.
_ Accept another pair of a and b.
_ .....

oh, the problem is i can't break the loop, if i do, then the program will work like:

_ Accept a and b.
_ Print out a/b or error if found.

My program needs to work like:

_ Accept a and b.
_ Print out a/b or error if found.
_ Accept another pair of a and b.
_ .....

But what's the need of using signal then.

just check if(b==0) throw error.

e.g

if(b==0)
{
printf("error\n");
continue;
}
else
{
//find out a/b here
}

A brief note on signals and calling standard library functions:
http://groups.google.com/group/comp.lang.c/browse_thread/thread/00d5082d648b2437/a4d67511412817da#a4d67511412817da

[edit]Doing some more digging, I found this:

The function func may terminate by executing a return statement or by calling the abort, exit or longjmp functions. If func executes a return statement, and the value of sig was SIGFPE or any other implementation-defined value corresponding to a computational exception, the behavior is undefined. Otherwise, the program will resume execution at the point it was interrupted.

I didn't know that -- I'm glad this question brought it up for me.

My tinkering ended up looking like this:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void sign_zeroError(int sig)
{
   puts("divide-by-zero signal received");
   exit(0);
}

int main()
{
   int a = 5, b = 2;
   signal(SIGFPE, sign_zeroError);
   for ( ;; )
   {
      int result = a / b;
      printf("%d / %d = %d\n", a, b, result);
      --b;
   }
   return 0 ;
}

/* my output
5 / 2 = 2
5 / 1 = 5
divide-by-zero signal received
*/
Comments
Earned corn today :)
This article has been dead for over six months. Start a new discussion instead.