(转载)Linux中调用system()的返回值

(转载)http://www.cnblogs.com/hjslovewcl/archive/2011/06/29/2314321.html

先写一个被调用的函数

#include <stdio.h>

int main(int argc, char* argv[])
{
    return 10;
}

[root@robot ~]# gcc test_return.c -o test_return
[root@robot ~]# ./test_return
[root@robot ~]#

再写一个调用system的程序

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>

int main()
{
    pid_t status;
    int errno;

    status = system("./test_return");
    printf("WIFEXITED(status):%d\n", WIFEXITED(status));
    printf("WEXITSTATUS(status):%d\n", WEXITSTATUS(status));

    if (status == -1)
    {
        printf("system error!");
    }

    if (WIFEXITED(status))
    {
        printf("cp exit normal![%d]\n", errno);
        printf("exit staus = [%X]\n", WEXITSTATUS(status));
    }
    else
    {
        printf("cp exit illegal![%d]\n", errno);
    }
}

程序输出:

[root@robot ~]# gcc testexit.c
[root@robot ~]# ./a.out
WIFEXITED(status):1
WEXITSTATUS(status):10  // system调用发挥的值
cp exit normal![0]
exit staus = [A]
[root@robot ~]#

原文地址:https://www.cnblogs.com/Robotke1/p/3056901.html