C Signals

Beginner
7 min read
Updated: Nov 2026
1 example
<signal.h>

What Is a Signal in C?

A signal is a simple event notice delivered to a program. Common examples are an interrupt request or an abnormal termination request. The <signal.h> header lets a beginner install a handler, ignore a signal, or restore the default action.

The two functions in this series are signal() and raise(). Use signal() to choose the disposition, then use raise() to test that choice inside the same program.

Key idea.

Keep handlers tiny. The safest beginner pattern is to set a volatile sig_atomic_t flag and continue in normal code after the signal returns.

Common Signal Numbers

MacroTypical meaning
SIGINTInteractive interrupt request.
SIGTERMTermination request.
SIGABRTAbnormal termination request.
SIGFPEErroneous arithmetic operation.
SIGSEGVInvalid memory access.
SIGILLInvalid instruction.

Also remember the disposition macros: SIG_DFL for the default action, SIG_IGN to ignore the signal, and SIG_ERR for a failed signal() call.

The Tiny Handler Pattern

  1. Declare a flag. Use volatile sig_atomic_t so the handler can update it safely.
  2. Write a small handler. Set the flag and return. Do not call printf or malloc there.
  3. Install with signal(). Check for SIG_ERR before continuing.
  4. Test with raise(). Then read the flag in normal code.

Starter: Install, Raise, and Read a Flag

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

volatile sig_atomic_t got = 0;

void on_sigint(int sig) {
    (void)sig;
    got = 1;
}

int main(void) {
    if (signal(SIGINT, on_sigint) == SIG_ERR) {
        printf("install failed\n");
        return 1;
    }

    if (raise(SIGINT) != 0) {
        printf("raise failed\n");
        return 1;
    }

    printf("got = %d\n", (int)got);
    return 0;
}

How It Works

  1. Install the handler. signal(SIGINT, on_sigint) chooses the response for SIGINT.
  2. Raise the signal. raise(SIGINT) asks the current program to handle it now.
  3. Keep the handler tiny. Only the flag is updated.
  4. Print in normal code. After raise returns, main can safely report the flag.

Open the Function Guides

Frequently Asked Questions

A signal is a simple notification that something happened, such as an interrupt request. signal.h lets a program choose how to respond.
SIG_DFL restores the default action. SIG_IGN asks the program to ignore that signal.
A handler can interrupt normal code. The safest beginner pattern is to set a volatile sig_atomic_t flag and continue later in main.
Learn signal() to install a handler, then raise() to test it.
The C library provides a portable core, but exact behavior and available signal numbers can still vary. Keep examples simple.

Browse signal.h functions

Open raise() and signal() with full examples.

Function index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful