In this tutorial you will learn about the C program to shut down or turn off computer and its application with practical example.
C program to shut down or turn off computer
In this tutorial, we will learn to create a C program that will shut down or turn off the computer using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Header Libraries and its usage.
Shut down or Turn off computer
In c programming it is possible shut down or turn off with the help of very small amount of code . The C language has many types of header libraries which has supported function in them with the help of these files the programming is easy.
Shutting Pc with the help of c program is so easy.
Algorithm:-
1 2 3 |
1. Include stdlib.h 2. Call the System Function 3. Execute the Program. |
Program to shut down or turn off the computer :-
Program For Windows 7:-
1 2 3 4 5 6 7 8 9 |
#include <stdio.h> #include <stdlib.h> int main() { // this function will shut the pc system("c:\\windows\\system32\\shutdown /s"); return 0; } |
Note to shutdown immediately use “C:\\WINDOWS\\System32\\shutdown -s -t 0”. To restart use “-r” instead of “-s”.
Here, “shutdown /s /t x”; where x is the number of seconds after which shutdown will occur.
Program For Windows xp:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> #include <stdlib.h> int main() { char ch; printf("Do you want to shutdown your computer now (y/n)\n"); scanf("%c", &ch); if (ch == 'y' || ch == 'Y') // this function will shut the pc system("C:\\WINDOWS\\System32\\shutdown -s"); return 0; } |
Note to shutdown immediately use “C:\\WINDOWS\\System32\\shutdown -s -t 0”. To restart use “-r” instead of “-s”.
Here, “shutdown /s /t x”; where x is the number of seconds after which shutdown will occur.
Program for Ubuntu:-
1 2 3 4 5 6 7 |
#include <stdio.h> int main() { // this function will shut the pc // linux os command for the shutting down of the pc system("shutdown -P now"); return 0; } |
Output:-
The program we have to first include stdlib.h and then we call the system function.