how do run a cprogram on shutdown or restart

That's platform specific. You need to tell your OS to run that program on boot. What OS are you using?

linux

If you don't care about run levels, placing a symbolic link to your executable in /etc/rc.d/rc.local (or /etc/rc.local depending on your distro) should do it.

Edit the /etc/rc.d/rc.local file and add a line to run the program with the actual path like :
/home/sam/program (where program is the binary name)
Otherwise, you can copy the binary to the /usr/bin directory (or a sym link will also do) and add only the executable name instead of the actual path.

But I doubt whether it will run before user login or after. Check it.

Here's a copy of my /etc/rc.local.shutdown

#!/bin/bash
#
# /etc/rc.local.shutdown: Local shutdown script.
#

#empty trash
/usr/bin/etrash

#remove /tmp files
rm -rf /tmp/*

You may also want to look into setting up Cron jobs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.