How to shutdown pc using c program

In this post, we will get to know how we can shut down our pc using c program, The header file stdlib.h is used to run an executable file shutdown.exe which is present in C:\WINDOWS\system32 the folder in Windows 7 and XP.

Below is the code to shut down your windows 7,8,10 pc

#include<stdio.h>
#include<stdlib.h>    // contains system method

int main()
{
    char ch; // declare a character 
    
    printf("Do you want to shutdown your pc now (y/n)?"); // show message
    scanf("%c", &ch); // takes input

    if(ch == 'y'|| ch == 'Y')
    {   /*
            /s is used to order the compiler 
            to shutdown the PC
        */
        system("C:\\WINDOWS\\System32\\shutdown /s");
    }

     return 0;
}

we can use various things while executing shutdown.exe, for example, you can use /t option to specify the number of seconds after which the shutdown happens.

  • 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 shut down immediately you can write "shutdown /s /t 0"

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

Below is the code to restart your windows 7,8,10 pc

#include<stdio.h>
#include<stdlib.h>    // contains system method

int main()
{
    char ch; // declare a character 
    
    printf("Do you want to shutdown your pc now (y/n)?"); // show message
    scanf("%c", &ch); // takes input

    if(ch == 'y'|| ch == 'Y')
    {   /*
            /s is used to order the compiler 
            to restart the PC
        */
        system("C:\\WINDOWS\\System32\\shutdown /r");
    }

     return 0;
}

You can see the execution of the code in the below video

Video language is in hindi

0 0 votes
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x