C/C++ program to shutdown a system?

Here we will see how we can shut down the system by writing a simple C program. The shutdown process varies in different operating systems. If we are Linux user, we can use this terminal command to shut down −

shutdown -P now

If we are using Windows system, we can use this command −

c:\windows\system32\shutdown /i
Note: These programs require administrator/root privileges to execute successfully. On Linux, you may need to run with sudo. On Windows, run the program as administrator.

Syntax

int system(const char *command);

The system() function executes a command specified in the command string and returns after the command has been completed.

Example 1: Linux System Shutdown

This program shuts down a Linux system immediately using the shutdown command −

#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("Shutting down Linux system...\n");
    system("shutdown -P now");
    return 0;
}

Example 2: Windows System Shutdown

This program opens the Windows shutdown dialog using the shutdown command −

#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("Opening Windows shutdown dialog...\n");
    system("c:\windows\system32\shutdown /i");
    return 0;
}

Alternative Windows Commands

You can also use these Windows shutdown commands for different behaviors −

  • shutdown /s /t 0 − Immediate shutdown
  • shutdown /r /t 0 − Immediate restart
  • shutdown /h − Hibernate
  • shutdown /l − Log off

Key Points

  • The system() function executes OS-specific commands from within C programs.
  • These programs require elevated privileges to execute system shutdown commands.
  • The commands are platform-specific and won't work across different operating systems.

Conclusion

System shutdown can be implemented in C using the system() function with OS-specific commands. Always ensure proper privileges and user confirmation before executing shutdown operations in production code.

Updated on: 2026-03-15T10:55:05+05:30

888 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements