title: How to print errno while saving the value from function?
body: <p>I am not able to open/create a file properly using open() function, so I thought that using errno messages will help me to figure out the reason. However I don't know how to set the if(), so it prints me the error.
  I know the code like this should work:</p>
  
  <pre><code>if(open(handle,O_RDWR | O_CREAT) == -1){
        printf("%s\n",strerror(errno));
  }
  </code></pre>
  
  <p>but what if I want to save the value from open() to my variable and IF it is -1 then print the error as well? I don't want to call open() twice for this and I don't know how to assign it if it is ok and how to print error if not.</p>
  
  <pre><code> #include &lt;stdio.h&gt;
      #include &lt;stdlib.h&gt;
      #include &lt;fcntl.h&gt;
      #include &lt;ctype.h&gt;
      #include &lt;sys/types.h&gt;
      #include &lt;sys/stat.h&gt;
      #include &lt;string.h&gt;
      #include &lt;errno.h&gt;
  
  
      int main(int argc, char **argv){
          int handle,i,help;
          int *array = malloc(5*sizeof(int));
          float *red = malloc(5*sizeof(int));
  
          array[0]=5;
          array[1]=4;
          array[2]=3;
          array[3]=2;
          array[4]=1;
  
          handle = open(argv[1], O_RDWR | O_CREAT);
          if(handle == -1){
              printf("%s\n",strerror(errno));
          }
  
          printf("handle 1 %d\n",handle);
          write(handle,array,20);
          close(handle);
  
          handle = open(argv[1], O_RDONLY);
          lseek(handle,0,SEEK_SET);
          printf("handle %d\n",handle);
          help=read(handle,&amp;red,20);
          printf("pomoc %d\n",help);
          for(i=0; i&lt;5; i++){
              printf("%d\n",(int)red[i]);
          }
          close(handle);
          return 0;
      }
  </code></pre>