How to use the system() function in C language?

青灯夜游
Release: 2020-08-29 11:28:32
Original
14202 people have browsed it

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 "".

How to use the system() function in C language?

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 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 <stdlib.h>

Define function:

int system(char *command);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template