C language programming simple timing shutdown procedures

  • 2020-05-09 18:54:21
  • OfStack

Write a regular shutdown small program, can immediately shut down the computer, also can 1 period of time after the shutdown of the computer.

The main focus here is the system() command.

Code implementation:


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
  char cmd[20]="shutdown -s -t ";
  char t[5]="0";
  int c;
  system("title C Language shutdown program "); // Set up the cmd The window title 
  system("mode con cols=48 lines=25"); // Window width and height  
  system("color f0"); // Can be written as  red  Bring up the color group 
  system("date /T");
  system("TIME /T");
  printf("----------- C Language shutdown program  -----------\n");
  printf("1. implementation 10 Shut down the computer at a timer within minutes \n");
  printf("2. Shut down the computer immediately \n");
  printf("3. Log off computer \n");
  printf("0. Log out \n");
  printf("-------------------------------------\n");
  scanf("%d",&c);
  switch(c) {
    case 1:
      printf(" How many seconds do you want to automatically shut down your computer? ( 0~600 ) \n");
      scanf("%s",t);
      system(strcat(cmd,t));
      break;
    case 2:
      system("shutdown -p");
      break;
    case 3:
      system("shutdown -l");
      break;
    case 0:
      break;
    default:
      printf("Error!\n");
  }
  system("pause");
  return 0;
}

This program is not very useful, but it gives us an idea of the system() function.

Under Windows, the system() function can execute the dos command. In Unix/Linux, Shell can be executed.

Please run the above program under Windows. The setup and shutdown functions of the dos interface in the program are realized through the dos command.


Related articles: