C: Doing something when the program exits

In C, if my application ends unexpectedly can I call a function before that happens? I’m writing a flag into a database (processRunning = 1) that prevents other applications from starting a similar …