C program to shutdown or turn off computer


Levels of difficulty: / perform operation:

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:\WINDOWS\system32 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 Program

Exp 1
#include<stdio.h>
#include<conio.h>
main() {
	int number;
	printf("
	Enter an integer\n"
	);
	scanf("
	%d"
	,&
	number);
	printf("
	Integer entered by you is %d\n"
	, number);
	return 0;
}

C programming code for Windows 7

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

C programming code for Ubuntu Linux

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