How to turnoff or restart your PC using a C Program
This article will help you to invoke system level commands and shutdown your pc using programs. Invoking system level commands will help you learn more about how your operating system works and helps you to create events in your system, this will also help you to automate tasks.
It won’t take more than a minute to code and compile it. Moreover a newbie C programmer who knows some basics in C program can understand this program.
If you want mastering in C programming, You may learn from a pro on your own schedule
C Program to shutdown your computer: This program turn off i.e shutdown your computer system. Firstly it will asks you to shutdown your computer if you press ‘y’ then your computer will shutdown in 30 seconds, system function of “stdlib.h” is used to run an executable file shutdown.exe which is present in C:WINDOWSsystem32 in Windows XP. You can use various options while executing shutdown.exe, you can use -t option to specify number of seconds after which shutdown occurs.
Syntax: shutdown -s -t x; here x is the number of seconds after which shutdown will occur.
By default shutdown occur after 30 seconds.To shutdown immediately you can write “shutdown -s -t 0”. If you wish to restart your computer then you can write “shutdown -r”.
C programming code for Windows XP
C programming code for Windows 7 and above
C programming code for Ubuntu Linux
You need to be logged in as root user for above program to execute otherwise you will get the message shutdown: Need to be root, now specifies that you want to shutdown immediately. ‘-P’ option specifies you want to power off your machine. You can specify minutes as:
shutdown -P “number of minutes”.
If you like this Program and find this helpful, don't forget to like and share with your friends!