title: Unable to maintain previous value of variable into another variable in C?
body: <p>I have tried to make demo code of my actual code. I am having 2 gloabal variables prevValue and currentValue . After some time interval value of "currentValues" may get change (either x, y or z).</p>
  
  <p>My aim is to <strong>maintain value of "currentValue" variable from earlier stage(N-1) into "prevValue" variable in the current stage(N)</strong> and so on. for example , suppose I am in stage 3, so I want to store value of "currentValue" variable from earlier stage2 i.e. 50 into stage3 "prevValue" variable.  But I am not getting expected result. So how do I correct it? </p>
  
  <p>Expected output</p>
  
  <p>stage1: prevSt = 10 and currentSt =10</p>
  
  <p>stage2: prevSt = 10 and currentSt =50</p>
  
  <p>stage3: prevSt = 50 and currentSt =10</p>
  
  <p>stage4: prevSt = 10 and currentSt =70</p>
  
  <p>stage5: prevSt = 70 and currentSt =50</p>
  
  <p>Actual output</p>
  
  <p>stage1: prevSt = 10 and currentSt =10</p>
  
  <p>stage2: prevSt = 10 and currentSt =50</p>
  
  <p>stage3: prevSt = 10 and currentSt =10</p>
  
  <p>stage4: prevSt = 10 and currentSt =70</p>
  
  <p>stage5: prevSt = 10 and currentSt =50</p>
  
  <p><strong>NOTE:</strong> Store or maintain the value of the variable in the sense that I want to use previous value of variable from the earlier stage into the current one till that stage only. (when there is stage 2 going on I want previous value from stage 1 , when there is stage 3 going on I want previous value from stage 2 and so on). Also there is no concern whether we write the logic for this into the same main function or separate function like I have written above </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;time.h&gt;
  
  int prevValue,currentValue;
  int prevStateValue(int x, int y, int z, int pValue ,int cValue)
  {
      if(cValue != pValue &amp;&amp; cValue == x)
      {
          if(pValue == y)
          {
              pValue = y;
          }
          else
          {
              pValue = z;
          }
      }
      else if(cValue != pValue &amp;&amp; cValue == y)
      {
          if(pValue == x)
          {
              pValue = x;
          }
          else
          {
              pValue = z;
          }
      }
      else
      {
          if(pValue == x)
          {
              pValue = x;
          }
          else
          {
              pValue = y;
          }
      }
      return pValue;
  }
  
  int main(void)
  {
      int x ,y,z;
      x=10;
      y=50;
      z=70;
  
  //stage 1
      currentValue = x;
      prevValue = currentValue ;
      sleep(1);
      prevValue = prevStateValue(10 ,50 ,70 ,prevValue ,currentValue);
      printf("stage1: prevSt = %d and currentSt =%d\n\r",prevValue,currentValue);
  
  //stage 2   
      currentValue = y;
      sleep(1);
      prevValue = prevStateValue(10 ,50 ,70 ,prevValue ,currentValue);
      printf("stage2: prevSt = %d and currentSt =%d\n\r",prevValue,currentValue);
  
  //stage 3
      currentValue = x;
      sleep(2);
      prevValue = prevStateValue(10 ,50 ,70 ,prevValue ,currentValue);
      printf("stage3: prevSt = %d and currentSt =%d\n\r",prevValue,currentValue);
  
  //stage 4
      currentValue = z;
      sleep(1);
      prevValue = prevStateValue(10 ,50 ,70 ,prevValue ,currentValue);
      printf("stage4: prevSt = %d and currentSt =%d\n\r",prevValue,currentValue);
  
  //stage 5
      currentValue = y;
      sleep(1);
      prevValue = prevStateValue(10 ,50 ,70 ,prevValue ,currentValue);
      printf("stage5: prevSt = %d and currentSt =%d\n\r",prevValue,currentValue);
  
  return 0;
  }
  </code></pre>