Signup/Sign In

C Program to shutdown Windows/Linux Shutdown Machine

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.

  • Syntax: "shutdown /s /t x"; here x is the number of seconds after which shutdown will occur.
  • Example: By default shutdown occurs after 30 seconds. To shutdown immediately you can write "shutdown /s /t 0"

If you wish to restart your computer then you can use "shutdown /r".


Program to Shutdown Windows XP Machine

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;
}
  • To shutdown immediately use "C:\\WINDOWS\\System32\\shutdown -s -t 0". To restart use "-r" instead of "-s".
  • For better understanding go through the program for shutting down Windows 7, in which there is a detailed explanation of using t and r instead of s.

Note: A '-' performs the same function in Windows XP as that performed by '/' incase of Windows 7.


Program to Shutdown Linux OS

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;
}
  • You need to be logged in as user for above program to execute otherwise you will get the message shutdown: "Need to be root".
  • '-P' option specifies you want to power off your machine.
  • You can specify minutes as: shutdown -P "number of minutes"
  • For more help or options type at terminal: man shutdown