How to write a program to shutdown a system in C/C++
How to shut down the computer in Linux and/or Windows?
The idea is to use system() in C language. This function is used to call operating system commandsfrom a C program.
Windows OS:
Linux OS:
// C program to shutdown in Linux
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Running Linux OS command using system
system("shutdown -P now");
return 0;
}
Windows OS:
Shutdown/ Logoff/ Restart a Windows OS
We will use system() in <stdlib.h> to perform system operations with the help of C programs.To perform any of the above system operations, we will code as follows:
#include <stdio.h> #include <stdlib.h> int main() { system("c:\\windows\\system32\\shutdown /i"); return 0; }
The argument to the system function is the path to OS and /i is one of the entities from the vast options available to us. To view the options, we run cmd and type:
C:\Users\User>shutdown
To perform different operations, we just replace the last “/path” in system() argument.The common operations are:
Shutdown
system("c:\\windows\\system32\\shutdown /s");Restartsystem("c:\\windows\\system32\\shutdown /r");Logoffsystem("c:\\windows\\system32\\shutdown /l");
Comments
Post a Comment