C++ Standard Library C++ STL Library

C++ <csignal> - SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM



The C++ <csignal> SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM macro constants expands to an integer constant expression with distinct values, which represent different signals sent to the program.

MacrosDescription
SIGABRT (Signal Abort) Abnormal termination, such as is initiated by the abort() function.
SIGFPE (Signal Floating-Point Exception) Erroneous arithmetic operation, such as zero divide or an operation resulting in overflow (not necessarily with a floating-point operation).
SIGILL (Signal Illegal Instruction) Invalid program image, such as an illegal instruction. This is generally due to a corruption in the code or to an attempt to execute data.
SIGINT (Signal Interrupt) Interactive attention signal. Generally generated by the application user.
SIGSEGV (Signal Segmentation Fault) Invalid memory access: When a program tries to read or write outside the memory it has allocated.
SIGTERM (Signal Terminate) Termination request sent to program.

Definition in the <csignal> header file is:

#define SIGABRT    /* implementation defined */
#define SIGFPE     /* implementation defined */
#define SIGILL     /*implementation defined */
#define SIGINT     /* implementation defined */
#define SIGSEGV    /* implementation defined */
#define SIGTERM    /* implementation defined */

Example:

The example below shows how the SIGINT macro constant is used to send signal to a program.

#include <csignal>
#include <cstdio>
 
volatile sig_atomic_t gSignalStatus = 0;
 
void signal_handler(int signal) {
  gSignalStatus = signal;
}
 
int main(void) {
  //installing a signal handler
  signal(SIGINT, signal_handler);

  printf("SignalValue: %d\n", gSignalStatus);
  printf("Sending signal %d\n", SIGINT);
  raise(SIGINT);
  printf("SignalValue: %d\n", gSignalStatus);

  return 0;
}

The output of the above code will be:

SignalValue: 0
Sending signal 2
SignalValue: 2

❮ C++ <csignal> Library