Home>Article>Backend Development> How to use the system() function in C language?
In C language, the system() function is used to execute shell commands, that is, to send an instruction to dos; usage: "int system(char *command);". The system() function needs to be called with the header file "
".
system() is a C/C function. The detailed explanation of the system () function under the Windows operating system is mainly applied in C language. The system function needs to be added with the header file 8e359799bdf1a571032ba13cc96acda9 before it can be called.
system() is used to execute shell commands, that is, to send an instruction to dos.
system("pause") can freeze the screen to facilitate observing the execution results of the program; system("CLS") can clear the screen. Calling the color function can change the foreground color and background of the console. The specific parameters are explained below.
For example, use system("color 0A"); where the 0 after color is the background color code, and A is the foreground color code. The color codes are as follows:
0=black 1=blue 2=green 3=lake blue 4=red 5=purple 6=yellow 7=white 8=grey 9=light blue A=light green B=light green C=light red D=light purple E=light yellow F=bright white
Related functions: fork, execve, waitpid, popen
Header file:#include 8e359799bdf1a571032ba13cc96acda9
Define function:
int system(char *command);
Parameters
command -- A C string containing the name of the requested variable.
system() will call fork() to generate a child process. The child process will call /bin/sh-c string to execute the command represented by the parameter string string. This command will return immediately after execution. The original calling process. During the call to system(), the SIGCHLD signal will be temporarily shelved, and the SIGINT and SIGQUIT signals will be ignored.
Return value
1. If system() is in If calling /bin/sh fails, 127 will be returned, and -1 will be returned for other failure reasons.
2. If the parameter string is a null pointer (NULL), a non-zero value will be returned.
3. If the system() call is successful, the return value after executing the shell command will be returned. However, this return value may also be 127 returned when system() fails to call /bin/sh, so it is best to check errno again to confirm that the execution is successful.
Related recommendations:c language tutorial video
The above is the detailed content of How to use the system() function in C language?. For more information, please follow other related articles on the PHP Chinese website!