How to Shutdown PC using C++ ?
ยท
184 words
ยท
1 minute read
If you want to shutdown your Windows PC from your C++ code, use this code snippet.
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("c:\\Windows\\system32\\shutdown /s");
return 0;
}
Here’s a breakdown of what this code does:
1. Includes ๐
<stdio.h>: This header file provides standard input/output functions likeprintf. However, it’s not directly used in this code.<stdlib.h>: This header file provides general utility functions, includingsystemwhich is used in this code.
2. main function ๐
This is the entry point of the program.
3. system("c:\\Windows\\system32\\shutdown /s"); ๐
system: This function takes a command string as an argument and executes it as a shell command."c:\\Windows\\system32\\shutdown /s": This is the command string being passed tosystem. Let’s break it down further:c:\\Windows\\system32\\shutdown: This specifies the location of theshutdown.exeprogram on a typical Windows system./s: This is a flag for theshutdowncommand. The/sflag instructs the computer to shut down.
4. return 0; ๐
This line indicates successful program termination by returning 0 from the main function.
I hope this helps. Do you recommend reading this blog post? share it!