C raise() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
signal.h

Definition

In C, raise() asks the program to handle one signal number right now. It is declared in <signal.h> and is useful for testing a handler you installed with signal().

You pass one signal number, such as SIGINT or SIGTERM. On success the call returns 0. On failure it returns a nonzero value. Keep handlers simple, and check the result before assuming the signal was delivered.

Syntax

Pass the signal number you want the current program to handle.

C
raise(SIGINT);
CallWhat it does
raise(SIGINT);Requests that the current program handle SIGINT now.

Include <signal.h>. Pair raise() with a handler installed by signal().

Library declarations
C
int raise(int sig);

Parameters

ParameterDescription
sigRequired. The signal number to raise, such as SIGINT, SIGTERM, or SIGABRT.

Return Value

Return ValueDescription
0Success. The signal was raised for the current program.
nonzeroFailure. The signal could not be raised.

C Examples

Example 1 — Raise SIGINT After Installing a Handler

Install a handler for SIGINT, then raise that signal so the handler runs.

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

volatile sig_atomic_t got_signal = 0;

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

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

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

    if (got_signal) {
        printf("SIGINT handled\n");
    }
    return 0;
}

How It Works

  1. Install a handler. signal(SIGINT, on_sigint) tells the program how to respond to SIGINT.
  2. Raise the same signal. raise(SIGINT) asks the current program to handle SIGINT now.
  3. Keep the handler tiny. The handler only sets a volatile sig_atomic_t flag.
  4. Continue in main. After raise() returns, normal code can safely print the result.

Example 2 — Check Whether raise() Succeeded

Always inspect the return value before trusting that the signal was delivered.

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

int main(void) {
    int status = raise(SIGTERM);

    if (status == 0) {
        printf("raise succeeded\n");
    } else {
        printf("raise failed\n");
    }
    return 0;
}

How It Works

  1. Call raise. Pass a standard signal number such as SIGTERM.
  2. Store the result. Keep the return value in an int so you can test it.
  3. Compare with zero. Zero means success. Any nonzero value means failure.
  4. Branch clearly. Print different messages so the success path is obvious in beginner programs.

Example 3 — Count How Many Times a Signal Was Raised

Raise the same signal more than once and count the handler runs with a flag counter.

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

volatile sig_atomic_t hits = 0;

void on_user(int sig) {
    (void)sig;
    hits += 1;
}

int main(void) {
    signal(SIGABRT, on_user);

    raise(SIGABRT);
    raise(SIGABRT);

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

How It Works

  1. Use a counter flag. hits is a volatile sig_atomic_t, which is safe to update from a simple handler.
  2. Install one handler. Both raises use the same SIGABRT handler.
  3. Raise twice. Each successful raise(SIGABRT) increments the counter.
  4. Print after returns. Reading and printing hits happens in normal code after the raises finish.

Frequently Asked Questions

raise() sends a signal to the current program so its handler for that signal can run.
sig is required. It is the signal number, such as SIGINT or SIGTERM.
On success it returns 0. On failure it returns a nonzero value.
Include <signal.h>.
signal() installs or changes a handler. raise() asks the current program to handle a signal now.

Next: signal()

Install, ignore, or restore the default action for a signal.

signal() 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