温馨提示×

c语言函数system怎么调用

小亿
120
2023-10-11 05:04:12
栏目: 编程语言

在C语言中,可以使用system()函数来调用系统命令。system()函数的原型如下:

int system(const char *command);

参数command是要执行的命令字符串。system()函数会将command字符串传递给操作系统执行,并等待命令执行完毕后返回。返回值是命令的退出状态码。

以下是一个简单的示例,演示如何使用system()函数调用系统命令:

#include <stdio.h>
#include <stdlib.h>
int main() {
int result = system("ls -l"); // 执行ls -l命令
printf("命令执行完毕,退出状态码:%d\n", result);
return 0;
}

在上述示例中,system()函数调用了ls -l命令,输出当前目录下的文件列表。执行完命令后,会打印命令的退出状态码。

0