This program turns off i.e shutdown your computer system. System function of stdlib.h
is used to run an executable file shutdown.exe which is present in C:\WINDOWS\system32
folder in Windows 7 and XP.
Below is a program to shutdown Windows 7.
#include<stdio.h>
#include<stdlib.h> // to use system() method
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char ch;
printf("Do you want to shutdown your pc now (y/n)?");
scanf("%c", &ch);
if(ch == 'y'|| ch == 'Y')
{ /*
/s is used to order the compiler
to shutdown the PC
*/
system("C:\\WINDOWS\\System32\\shutdown /s");
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
You can use various options while executing shutdown.exe, for example you can use /t
option to specify number of seconds after which the shutdown occurs.
"shutdown /s /t x";
here x is the number of seconds after which shutdown will occur."shutdown /s /t 0"
If you wish to restart your computer then you can use "shutdown /r"
.
Below is a program to shutdown Windows XP.
#include<stdio.h>
#include<stdlib.h> // to use system() function
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char ch;
printf("Do you want to shutdown the PC- (y/n) ?\n");
scanf("%c", &ch);
if(ch == 'y' || ch == 'Y')
{
system("C:\\WINDOWS\\System32\\shutdown -s");
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
"C:\\WINDOWS\\System32\\shutdown -s -t 0"
. To restart use "-r"
instead of "-s"
.r
instead of s
.Note: A '-' performs the same function in Windows XP as that performed by '/' incase of Windows 7.
Below is a program to shutdown Linux operating system.
#include<stdio.h>
#include<stdlib.h> // to use system() function
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char ch;
printf("Do you want to shutdown your pc now(y/n)?");
scanf("%c", &ch);
if(ch == 'y' || ch == 'Y')
system("shutdown -P now");
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
'-P'
option specifies you want to power off your machine.shutdown -P "number of minutes"
man shutdown