300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > c语言用命令行编译运行程序_使用C程序执行系统命令

c语言用命令行编译运行程序_使用C程序执行系统命令

时间:2021-10-11 09:18:16

相关推荐

c语言用命令行编译运行程序_使用C程序执行系统命令

c语言用命令行编译运行程序

Sometimes, we may need to execute Linux/Windows DOS commands through our C program. (Note: the code given below is compiled and executed on Linux GCC compiler, so here we are testing Linux commands only).

有时,我们可能需要通过C程序执行Linux / Windows DOS命令 。 (注意:下面给出的代码是在Linux GCC编译器上编译和执行的 ,因此这里我们仅测试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()函数之后编写的下一条语句。

C中的system()函数 (system() function in C)

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);

在C程序中运行Linux命令的程序 (Program to run Linux commands within C program)

#include <stdio.h>#include <stdlib.h>//to use system()#include <string.h> //to use strcpy()int main(){char *command;//executing ls commandstrcpy(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/executing-system-commands-using-c-program.aspx

c语言用命令行编译运行程序

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。