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 like printf. However, it’s not directly used in this code.
  • <stdlib.h>: This header file provides general utility functions, including system which 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 to system. Let’s break it down further:
    • c:\\Windows\\system32\\shutdown: This specifies the location of the shutdown.exe program on a typical Windows system.
    • /s: This is a flag for the shutdown command. The /s flag 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!

Share:
waffarx cash back