title: fibonacci using fork() in the child process
body: <p>I wrote the code below for homework purposes. When I run it on XCode in OSX, after the sentence "Enter the number of a Fibonacci Sequence:", I enter the number 2 times. Why 2 and only 1 <code>scanf</code>.</p>
  
  <p>The code : </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
  
  int main()
  
  {
  
  
  
  int a=0, b=1, n=a+b,i;
  
  
  printf("Enter the number of a Fibonacci Sequence:\n");
  scanf("%d ", &amp;i);
  
  pid_t pid = fork();
  if (pid == 0)
  {
      printf("Child is make the Fibonacci\n");
      printf("0 %d ",n);
      while (i&gt;0) {
          n=a+b;
          printf("%d ", n);
          a=b;
          b=n;
          i--;
          if (i == 0) {
              printf("\nChild ends\n");
          }
      }
  }
      else 
      {
          printf("Parent is waiting for child to complete...\n");
          waitpid(pid, NULL, 0);
          printf("Parent ends\n");
      }
      return 0;
  }
  </code></pre>