Usage of system () function in C __ function

Source: Internet
Author: User
Tags clear screen
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; 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.