C语言定时关机小程序

时间:2022-07-22
本文章向大家介绍C语言定时关机小程序,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

C语言定时关机小程序

这个关机小程序的核心是对system函数的应用,大家可能感觉学了很久的C依然对着黑色的控制台程序,而system函数就比较意思了,其实说白了system函数执行的是windows中的dos命令,窗口设置和关机的操作都是,而unix和linux是shell命令,大家有兴趣的可以了解下,不说了,上源码!

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(void)
{
    char cmd[20]="shutdown -s -t ";
    char t[5]="0";
    int c;

    system("title C语言定时关机程序");  //设置cmd窗口标题
    system("mode con cols=50 lines=30");  //窗口设置 
    system("color 84");  //可以任意设定颜色
    system("date /T");
    system("TIME /T");

    printf("----------- C语言定时关机程序 -----------n");
    printf("1.实现10分钟内的定时关闭计算机n");
    printf("2.立即关闭计算机n");
    printf("3.注销计算机n");
    printf("0.退出系统n");
    printf("-------------------------------------n");

    scanf("%d",&c);
    switch(c) {
        case 1:
            printf("你想在多少秒后自动关闭计算机?(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;
}