title: When creating a loading bar using printf and \r in c, how does one keep the "moving" code contained
body: <p>I'm creating a loading bar..and I almost have it but when the "=" sings add to themselves the end of my loading bar moves down the line as well. I want to stop this so instead of</p>
  
  <ul>
  <li><code>progress: |==== | 3%</code> </li>
  <li><code>progress: |============ |3%</code></li>
  </ul>
  
  <p>I want</p>
  
  <ul>
  <li><p><code>progress |== ............... | 3%</code></p></li>
  <li><p><code>progress |=========  .| 3%</code></p></li>
  </ul>
  
  <p>It's hard to see because of the formatting, but I don't want the second "|" line to move with the equal signs. </p>
  
  <p>Here's my code; I think needs a little tweaking:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  
  int main(int argc, char* argv[])
  {
      if (argc != 2)
      {
          printf("Usage: progress n\n");
          return 1;
      }
  
      int n = atoi(argv[1]);
  
      //printf("Enter a percentage: ");
      //scanf("%d", &amp;n);
      //progress: |==================                                |  37%
      //int percentage = 37; 
  
      char display[70];
      char equals[69];
      char disp[20];
  
      for (int i = 0; i &lt;=10* n; i++)
      {
          strcpy(display, "progress: |");
          strcat(equals, "=");
          strcpy(disp, "       |");
  
          char number[10];
          char space[10];
  
          sprintf(number, "  %d%%", i);
          sprintf(space, "%s", equals);
  
          strcat(display, space);
          strcat(display, disp);
          strcat(display, number);
  
          fprintf(stderr, "\r%s %d  \r%s",equals, i, display);
          usleep(1000000);
      }
  
      return 0;
  } 
  </code></pre>