Easy Tutorial
❮ C Exercise Example44 C Examples Quadratic Roots ❯

C Library Function - raise()

C Standard Library - <signal.h>

Description

The C library function int raise(int sig) generates the signal sig. The sig parameter is compatible with SIG macros.

Declaration

Here is the declaration for the raise() function.

int raise(int sig)

Parameters

Macro Signal
SIGABRT (Signal Abort) Abnormal termination of the program.
SIGFPE (Signal Floating-Point Exception) Arithmetic operation error, such as division by zero or overflow (not necessarily floating-point operations).
SIGILL (Signal Illegal Instruction) Illegal function image, such as illegal instruction, usually due to a variant in the code or an attempt to execute data.
SIGINT (Signal Interrupt) Interrupt signal, such as ctrl-C, usually generated by the user.
SIGSEGV (Signal Segmentation Violation) Illegal memory access, such as accessing a non-existent memory unit.
SIGTERM (Signal Terminate) Termination request signal sent to the program.

Return Value

The function returns zero if successful, otherwise it returns a non-zero value.

Example

The following example demonstrates the use of the raise() function.

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

void signal_catchfunc(int);

int main() {

   int ret;

   signal(SIGINT, signal_catchfunc);

   printf("Starting to generate a signal\n");
   ret = raise(SIGINT);
   if (ret != 0) {
      printf("Error, unable to generate SIGINT signal\n");
      exit(0);
   }

   printf("Exiting....\n");
   return 0;
}

void signal_catchfunc(int signal) {
   printf("Signal caught\n");
}

Let's compile and run the above program, which will produce the following result:

Starting to generate a signal
!! Signal caught !!
Exiting...

C Standard Library - <signal.h>

❮ C Exercise Example44 C Examples Quadratic Roots ❯