← Back to C Course | Chapter 14: Additional Topics | Lesson 6 of 9

C Signal Handling

A signal is a message from the system telling a program that something happened, like a doorbell ringing. C lets you choose what to do when it rings, such as when someone presses Ctrl+C.
Syntax
c
#include <signal.h>

void handler(int signum) {
    // respond to the signal
}

signal(SIGINT, handler);   // register handler for Ctrl+C

What is a Signal?

A signal is an asynchronous notification the operating system delivers to a running process to report an event like an illegal instruction, a segmentation fault, or the user pressing Ctrl+C, interrupting the program's normal flow so it can react to that event.

Example: What is a Signal?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Signals notify a process of events like Ctrl+C");
	// Return 0 to signal that the program finished successfully
	return 0;
}

The signal() Function

The signal() function registers a custom handler function to run whenever a specific signal (identified by a constant like SIGINT or SIGSEGV) is delivered to your process, letting you replace the operating system's default behavior — which is usually to terminate the program outright — with your own response.

Example: The signal() Function

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
void handler(int sig) {
	// Print formatted text to the screen
	printf("Caught signal %d", sig);
}
// Program execution starts in main()
int main() {
	signal(SIGINT, handler);
	raise(SIGINT);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Catching SIGINT

SIGINT is the signal generated when the user presses Ctrl+C in the terminal running your program, and catching it with a custom handler lets your program close open files, free resources, and shut down gracefully instead of being killed abruptly mid-operation.

Example: Catching SIGINT

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
void handleSigint(int sig) {
	// Print formatted text to the screen
	printf("Ctrl+C caught, cleaning up");
}
// Program execution starts in main()
int main() {
	signal(SIGINT, handleSigint);
	raise(SIGINT);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Ignoring Signals

Passing the special value SIG_IGN as the handler argument to signal() tells the operating system to simply discard a specific signal whenever it arrives, rather than acting on it — a way to make a section of your program deliberately immune to an interrupt like SIGINT.

Example: Ignoring Signals

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
// Program execution starts in main()
int main() {
	signal(SIGINT, SIG_IGN);
	// Print formatted text to the screen
	printf("SIGINT will now be ignored");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Raising Signals with raise()

The raise() function lets a program generate and send a signal to itself programmatically, which is useful for testing how your own signal handlers behave, or for deliberately triggering an internal fault-handling path from within the program's own logic.

Example: Raising Signals with raise()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
void handler(int sig) {
	// Print formatted text to the screen
	printf("Handler triggered by raise()");
}
// Program execution starts in main()
int main() {
	signal(SIGTERM, handler);
	raise(SIGTERM);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Doing heavy work, such as printf or malloc, inside a signal handler, which is not safe.
  2. Forgetting that signal behavior can vary across platforms.
  3. Handling SIGINT and not exiting, so the program becomes hard to stop.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.