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

static volatile bool *termination_flag_ptr = NULL;

static void trap_ctrl_c(int sig)
{
    if (termination_flag_ptr != NULL)
    {
        *termination_flag_ptr = true;
    }
}

static void calculate_something(void)
{
    volatile bool stop = false;
    // Регистрируем.
    termination_flag_ptr = &stop;
    
    unsigned long long int n = 0;
    printf("Calculating, press Ctrl+C to stop...\n");
    while (!stop)
    {
        // Делаем какие-нибудь тяжелые вычисления.
        ++n;
    }
    
    printf("Calculation was terminated at %llu\n", n);
}


int main(void)
{
    signal(SIGINT, trap_ctrl_c);
    calculate_something();
}
