/* Example for raise and signal */ #include <signal.h> #include <stdio.h> #include <stdlib.h> void div_zero(int val) { printf("Divide by zero detected!\n"); exit(EXIT_FAILURE); } int main() { float numerator = 3.0F; float denominator = 0.0F; if (signal(SIGFPE, div_zero) == SIG_ERR) { perror("Could not set signal SIGFPR"); abort(); } if (denominator == 0.0F) raise(SIGFPE); else printf("The result of the division is %g\n", numerator / denominator); return EXIT_SUCCESS; }
Divide by zero detected!
The following macros are special values for handler:
signal sets the response. handler must be declared with C linkage. When a signal occurs, the signal's behavior is reset to SIG_DFL, the function for signal is called and sig is passed to it.
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル