title: How to get the status of command run by system()
body: <p>I am using one system call in my c code</p>
  
  <pre><code>#include &lt;sys/stat.h&gt;
  #include &lt;stdio.h&gt;
  
  int
  main(int argc, char *argv[])
  {
      int a = system("./test12.out");  //here if i give any wrong command
      system("echo $?")
      printf("system return is %d",a);
  }
  </code></pre>
  
  <p>there isn't any test12.out file in my current folder. Now output is </p>
  
  <pre><code>sh: ./test12.out: No such file or directory
  0
  system return is 32512
  </code></pre>
  
  <p>Here is my shell command failed but how can I know that in my c code?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>So, can I do this </p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      int a = system("dftg");
  
      if(a == -1)
          printf("some error has occured in that shell command");
      else if (WEXITSTATUS(a) == 127)
          printf("That shell command is not found");
      else
          printf("system call return succesfull with  %d",WEXITSTATUS(a));
  }
  </code></pre>