codescracker


c

C Program to Shutdown Computer



« Previous Program C Tutorial »


Shutdown Computer in C

To shutdown your computer using C programming, just call the system() function which will call the cmd (for windows) or terminal (for Linux) to shutdown your computer. So inside the system() function just place your code (providing full path) to shutdown your computer.

C Programming Code to Shutdown Computer

The function system() of stdlib.h library is used to run an executable file i.e., shutdown.exe. You can also use various options while executing the file shutdown.exe, you can use -t option to specify number of seconds after which you want your computer to get shutdown.

Syntax to Shutdown Your Computer

Here is the syntax to shutdown your computer:

shutdown -s -t x

But you have to put full path to execute the shutdown.exe file in the function system().

Here are one by one C programming code to shutdown your computer for all platforms such as Windows 7, Windows XP, Linux. Let's first start with, how to shutdown your computer using C programming code for Windows 7.

Shutdown Computer in C for Windows 7

This C program will shutdown or turn off your computer (for windows 7 user).

C Programming Code to Shutdown Computer for Windows 7

#include<stdio.h>
#include<stdlib.h>
int main()
{
	system("C:\\Windows\\System32\\shutdown /s");
	return 0;
}

By default, shutdown occurs after 30 seconds. So to shutdown your computer immediately you can specify number of seconds as 0 as shown here in this program.

To shutdown your computer immediately, use the following C program:

#include<stdio.h>
#include<conio.h>
int main()
{
	system("C:\\Windows\\System32\\shutdown /s /t 0");
	return 0;
}

Shutdown Computer in C for Windows XP

Following C program turn off or shutdown your computer system for windows XP user. First, it will ask you to shutdown your computer, if you press 'y' then your computer will shutdown after 30 seconds time.

C Programming Code to Shutdown Computer for Windows XP

#include<stdio.h>
#include<stdlib.h>
int main()
{
	char ch;
	printf("Do you want to shutdown your computer (y/n) : ");
	scanf("%c",&ch);
	if(ch=='y' || ch=='Y')
	{
		system("C:\\Windows\\System32\\shutdown -s");
	}
	return 0;
}

Shutdown Computer in C for Linux

Following C program turn off or shutdown your computer for Linux User.

C Programming Code to Shutdown Computer for Linux

#include<stdio.h>
int main()
{
	system("shutdown -P now");
	return 0;
}

Note - To shutdown your computer (for Linux users), 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 that you want to power off your machine. You can specify minutes as : shutdown -P "number of minutes"

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program C Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test