本文共 1740 字,大约阅读时间需要 5 分钟。
c语言用命令行编译运行程序
Sometimes, we may need to execute / through our . (Note: the code given below is , so here we are testing Linux commands only).
有时,我们可能需要通过执行 / 。 (注意:下面给出的代码是 ,因此这里我们仅测试Linux命令)。
In the C programming standard library, there is a function named system () which is used to execute Linux as well as DOS commands in the C program.
在C编程标准库中,有一个名为system()的函数,该函数用于执行Linux以及C程序中的DOS命令。
A command can be assigned directly to the function as an argument and command may also input from the user and then assigned to the function, function will send command to the operating system’s particular terminal like Linux terminal or DOS commands terminal to execute, and after the execution, you will get your output and program’s execution returns to the next statement written after the system() function.
可以将命令作为参数直接分配给函数,也可以从用户输入命令,然后将其分配给函数,函数会将命令发送到操作系统的特定终端(例如Linux终端或DOS命令终端)以执行,然后执行,您将获得输出,程序的执行返回到在system()函数之后编写的下一条语句。
system() is a library function, which is defined in the stdlib.h header file. It is used to execute the Linux commands/Windows DOS commands.
system()是一个库函数,该函数在stdlib.h头文件中定义。 它用于执行Linux命令/ Windows DOS命令。
Syntax:
句法:
system(char *command);
Example:
例:
char *command = "ls"; system(command);
#include#include //to use system()#include //to use strcpy()int main(){ char *command; //executing ls command strcpy(command, "ls"); printf("ls command...\n"); system(command); //executing date command strcpy(command, "date"); printf("date command...\n"); system(command); return 0;}
Output
输出量
Please run this program at your machine
翻译自:
c语言用命令行编译运行程序
转载地址:http://oatzd.baihongyu.com/