linux C获取system执行返回值

 注意,是获取执行的返回值,执行结果的返回值,一般成功0,失败看情况

具体如下:

 #include "stdio.h"
 #include "unistd.h"
 #include <stdlib.h>
 #include <sys/wait.h>
 #include <sys/types.h>
  
 int main(int argc, char* argv[])
 {
       int ret = 0;
       
       while(1)
      {
         ret = system("ls") ;
         printf(WEXITSTATUS(ret)) ;
        sleep(2) ;
      }
     return 0;
}

 

效果如下:

原文地址:https://www.cnblogs.com/ChenChangXiong/p/11912164.html