The system () function in C language is mainly used to emit a DOS command, which is already included in the standard C library and can be called directly.
Demo
#include <stdlib.h>
#include <stdio.h>
int main (void)
{
printf ("About to spawn Command.com and run a DOS command\n ");
System ("dir");
return 0;
The above program is equivalent to running the dir command under DOS, and can get all the files in the current directory.
Another example:
System ("pause") can freeze the screen
System ("CLS") enables clear screen operation
Another important method is the color function, which can change the foreground and background colors of the console
System ("Color 0A") color after 0 is the background color code, a is the foreground color code. The color codes are as follows:
0= Black 1= Blue 2 = green 3= Lake Green 4= Red 5 = Purple 6 = Yellow 7 = White 8 = Gray 9 = light Blue
A= light green b= light green c= light red d= light purple e= light yellow f= bright white
Demo1:c language calls DOS command to implement timing shutdown
#include <stdio.h> #include <string.h> #include <stdlib.h> int print () {
printf ("╪╪╪╪╪╪╧╧╧╧╧╧╧╧╪╪╪╪╪╪\n");
printf ("╔═══╧╧c language Shutdown Program ╧╧═══╗\n");
printf ("║※1. Implement a timed shutdown of the computer ║\n within 10 minutes");
printf ("║※2. Turn off computer ║\n immediately");
printf ("║※3. Log off Computer ║\n");
printf ("║※0. Exit system ║\n");
printf ("╚═══════════════════╝\n");
return 0; } void Main () {System ("Title C language Shutdown Program")//Set CMD window title system ("mode con cols=48 lines=25");/window Width height system ("Co
Lor 0B ");
System ("date/t");
System ("time/t");
Char cmd[20]= "shutdown-s-T";
Char t[5]= "0";
Print ();
int C;
scanf ("%d", &c);
GetChar (); Switch (c) {case 1:printf ("How many seconds do you want to turn off the computer automatically?")
(0~600) \ n "); scanf ("%s ", t); system (strcat (CMD,T);
Case 2:system ("shutdown-p");
Case 3:system ("shutdown-l");
Case 0:break;
default:printf ("error!\n");
System ("pause");
Exit (0); }
Demo2: Delete files in C language, such as the location of the file: D:\123.txt
#include <stdlib.h>
#include <stdio.h>
int main (void)
{
system ("Del d:\123.txt");
return 0;