C PROGRAMMING

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

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button