title: How to create hard link in Linux from a C program
body: <p>We know we can create hard link in Linux using <code>ln file1 file2</code> which will make <code>file2</code> a hard link of <code>file1</code>. </p>
  
  <p>However when I try to do this by using a C program, I face issues. Below is the C code.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;unistd.h&gt;
  
  int main(int argc, char *argv[])
  {
      if ((strcmp (argv[1],"ln")) == 0 )
      {
              char *myargs[4];
              myargs[0] = "ln";
              myargs[1] = argv[3];
              myargs[2] = argv[4];
              myargs[3] = NULL;
              execvp(myargs[0], myargs);
              printf("Unreachable code\n");
      }
      return 0;
  }
  </code></pre>
  
  <p>After compiling this program with gcc I run it as below.</p>
  
  <pre><code>$ ./a.out ln file1 file2
  ln: failed to access ‘file2’: No such file or directory
  $       
  </code></pre>
  
  <p>Here <code>file1</code> exists and <code>file2</code> is the desired hardlink. </p>
  
  <p>Could anyone point where did I make mistake here.</p>
  
  <p>Thanks.</p>