C signal.h Header

Beginner
7 min read
Updated: Oct 2026
3 examples
Live preview

What Is signal.h?

<signal.h> is the C standard header for signals—asynchronous notifications such as Ctrl+C (SIGINT). Use signal() to install a handler and raise() to deliver a signal to the current program.

Remember
#include <signal.h>

signal(sig, handler)   → install what happens for sig
raise(sig)             → send sig to this process now

SIGINT   → usually Ctrl+C
SIG_DFL  → default action (often terminate)
SIG_IGN  → ignore the signal

handler: set a flag — do not call printf
UsePrefer
Install / change a handlersignal(sig, handler)
Test a handler in coderaise(sig)
Recoverable app errorsreturn codes / errno

How to Use Signals in C

Install a tiny handler, deliver or wait for the signal, then react in normal code.

  1. Add #include <signal.h>.
  2. Declare a volatile sig_atomic_t flag (and optionally a handler that only sets it).
  3. Call signal(SIGINT, handler) (or SIG_IGN / SIG_DFL).
  4. In the main path, call raise(SIGINT) to test—or wait for a real Ctrl+C—then check the flag and print or exit.
PieceRequired?Role
signal(sig, fn)Yes (to catch)Registers the handler for that signal
raise(sig)NoDelivers the signal for testing
volatile sig_atomic_tRecommendedSafe flag set inside the handler

Pseudocode

Pseudocode
flag = 0

handler(sig):
    flag = 1          // only simple, async-safe work

main:
    signal(SIGINT, handler)
    raise(SIGINT)     // or wait for real Ctrl+C
    if flag == 1:
        print / cleanup / exit
    else:
        continue

Cheat Sheet

Keep these facts handy while writing signal code.

TopicDetail
Header#include <signal.h>
Installsignal(sig, handler) → previous handler or SIG_ERR
Deliverraise(sig)
Common signalSIGINT (Ctrl+C)
Special handlersSIG_DFL (default), SIG_IGN (ignore)
Safe patternHandler sets flag; main loop reads flag
Unsafe in handlerprintf, malloc, most library I/O

Live Preview

Simulate what happens when SIGINT arrives: catch with a flag, ignore with SIG_IGN, or leave default behavior.

Browser simulation of signal/raise (same idea as the C examples below).

Result
signal(SIGINT, handler)
raise(SIGINT)
→ handler sets flag = 1
→ main sees flag
Caught SIGINT — handler ran safely

Worked Walkthrough — Catch SIGINT Safely

Trace installing a handler, raising SIGINT, and reacting in main without calling printf inside the handler.

Calculation
got_sigint = 0

signal(SIGINT, handle_sigint)
raise(SIGINT)
  → OS runs handle_sigint
  → got_sigint = 1
  → returns to raise / main

main checks got_sigint:
  true → print "Caught SIGINT…"
  (printing happens here, not in the handler) ✓

C Examples

Compile with gcc file.c -std=c11 -o out. Examples use raise() so output is reproducible without pressing Ctrl+C.

Example 1 — Catch SIGINT with a sig_atomic_t Flag

The safe beginner pattern: the handler only sets a flag; main prints the message.

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

static volatile sig_atomic_t got_sigint = 0;

void handle_sigint(int sig) {
    (void)sig;
    got_sigint = 1;
}

int main(void) {
    if (signal(SIGINT, handle_sigint) == SIG_ERR) {
        printf("Could not install SIGINT handler\n");
        return 1;
    }

    printf("Simulating Ctrl+C with raise(SIGINT)...\n");
    raise(SIGINT);

    if (got_sigint) {
        printf("Caught SIGINT (signal %d) — handler ran safely\n", SIGINT);
    }

    return 0;
}

How It Works

  1. Install the handler. signal(SIGINT, handle_sigint) registers what to do for Ctrl+C.
  2. Deliver the signal. raise(SIGINT) sends the same signal as a keyboard interrupt.
  3. Handler stays tiny. It only sets got_sigint = 1—no printf inside.
  4. Main reacts. After raise returns, main checks the flag and prints safely.

Example 2 — Ignore SIGINT with SIG_IGN

Compare a custom handler with SIG_IGN, which discards the signal.

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

static volatile sig_atomic_t caught = 0;

void handler(int sig) {
    (void)sig;
    caught = 1;
}

int main(void) {
    signal(SIGINT, handler);
    raise(SIGINT);
    printf("With handler: caught = %d\n", (int)caught);

    signal(SIGINT, SIG_IGN);
    caught = 0;
    raise(SIGINT);
    printf("With SIG_IGN: caught = %d (still 0)\n", (int)caught);

    return 0;
}

How It Works

  1. First raise. A custom handler runs → caught becomes 1.
  2. Switch to ignore. signal(SIGINT, SIG_IGN) tells the system to discard SIGINT.
  3. Second raise. The signal is delivered but not handled → caught stays 0.
  4. Restore later if needed. Use SIG_DFL to return to the default action (often terminate).

Example 3 — Count How Many Times a Handler Runs

Use raise in a loop to verify the handler fires each time.

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

static volatile sig_atomic_t count = 0;

void on_signal(int sig) {
    (void)sig;
    count++;
}

int main(void) {
    signal(SIGINT, on_signal);

    raise(SIGINT);
    raise(SIGINT);
    raise(SIGINT);

    printf("Handler called %d time(s)\n", (int)count);

    return 0;
}

How It Works

  1. Register once. signal(SIGINT, on_signal) installs the handler.
  2. Raise three times. Each raise(SIGINT) invokes on_signal.
  3. Increment safely. The handler only does count++ on a sig_atomic_t.
  4. Report in main. Printing happens after all raises—still outside the handler.

Edge Cases & Pitfalls

Check these before relying on signal handlers in real projects.

safety

No printf in handlers

Most library I/O and malloc are not async-signal-safe. Set a flag; do real work in the main path.

portability

signal() behavior varies

Some platforms reset the handler to default after one delivery. Prefer simple flag patterns; on Unix, production code often uses POSIX sigaction.

default

Default often terminates

With SIG_DFL, raising SIGINT may kill the process. Do not casually raise after restoring default in demos.

scope

Not for normal errors

Bad user input belongs with return codes and errno—not signals.

Performance Notes

ApproachCostBest for
Flag in handlerTiny write + poll in loopGraceful Ctrl+C shutdown
raise in testsOne kernel/user deliveryReproducible demos
Heavy work in handlerUnsafe / unpredictableAvoid entirely

Signal delivery is rare compared with normal code. Keep handlers minimal so your program stays correct under interruption—clarity beats cleverness here.

Key Takeaways

  • Role: catch or ignore asynchronous signals like SIGINT.
  • API: signal installs; raise delivers.
  • Safety: handler sets a sig_atomic_t flag only.
  • Specials: SIG_IGN ignores; SIG_DFL restores default.

One line: catch signals with a tiny flag handler—do the real work (print, cleanup, exit) back in normal code.

Frequently Asked Questions

signal.h declares the standard C signal interface: signal() to install a handler for a signal number, raise() to send a signal to the current process, and macros like SIGINT, SIGFPE, SIG_DFL, and SIG_IGN. Signals are asynchronous notifications (for example Ctrl+C).
signal(sig, handler) registers what happens when signal number sig arrives. raise(sig) delivers signal sig to the calling program right now—useful for testing handlers or reporting errors as signals.
Avoid it. printf is not guaranteed async-signal-safe and can deadlock if interrupted while holding a lock. The safe pattern: set a volatile sig_atomic_t flag in the handler, then check the flag in your main loop and print there.
Special handler values passed to signal(). SIG_DFL restores default behavior (often terminate the process). SIG_IGN ignores the signal. Use them instead of a custom function when you want default or no action.
sigaction, sigprocmask, and kill are POSIX extensions—not part of standard C signal.h. They offer more control on Linux/macOS. Standard C programs use signal() and raise(); production Unix code often prefers sigaction over signal().
SIGINT is the interrupt signal, usually sent when the user presses Ctrl+C in a terminal. Default action is to terminate the process. You can catch it with signal(SIGINT, handler) for graceful shutdown.

Next: stdarg.h

Learn how C functions accept a variable number of arguments.

stdarg.h tutorial →

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.

9 people found this page helpful