Here's an example of how to do what I think you want to do.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <ctype.h>
typedef void(* sig_func_t )(int);
volatile sig_atomic_t keep_looping = 1;
void sig_handler(int sig_number) {
switch (sig_number) {
case SIGINT:
keep_looping = 0;
break;
}
}
int should_continue(void) {
char line[256];
while (1) {
printf("Continue? (y/n) ");
fgets(line, sizeof line, stdin);
if (tolower(line[0]) == 'y') {
keep_looping = 1;
signal(SIGINT, sig_handler);
return 1;
}
if (tolower(line[0]) == 'n')
break;
}
return 0;
}
int main (void) {
sig_func_t sig_func;
sig_func = signal(SIGINT, sig_handler);
if (sig_func == SIG_ERR) {
perror("signal");
exit(EXIT_FAILURE);
}
unsigned n = 0;
printf("Starting...\n");
while (1) {
while (keep_looping)
n++;
printf("Current value: n=%u\n", n);
if (!should_continue())
break;
}
signal(SIGINT, sig_func);
return EXIT_SUCCESS;
}