title: Variable value changes after function call, without being used
body: <p>I have been debugging the following code for the past few hours. I realize it is probably something stupid, but can't seem to figure it out.</p>
  
  <p>In the second for loop (see debug printfs) the value of bitsA changes. I can't figure out why.</p>
  
  <p>EDIT: In case anyone was wondering what the code in sat_count_update is:</p>
  
  <pre><code>counter-&gt;taken = (bool)((1 &lt;&lt; counter-&gt;cbits - 1) &amp; counter-&gt;count) ;
  </code></pre>
  
  <p>File: SaturatingCounter.H                                                                             </p>
  
  <pre><code>#ifndef SATURATINGCOUNTER_H
  #define SATURATINGCOUNTER_H
  #include &lt;stdbool.h&gt;
  
  //Macro function to return taken/not taken of counter
  #define sat_count_taken(COUNTER) COUNTER-&gt;taken
  
  typedef struct sat_count{
          char count;
          char cbits;
          bool taken;
  } sat_count_t;
  
  //Initialize and return counter
  void sat_count_init(char bits, sat_count_t *counter){
  
          counter-&gt;count = 0;
          counter-&gt;cbits = bits;
          counter-&gt;taken = false;
  
  }
  
  //Update taken member of sat_counter_t based on count
  void sat_count_update(sat_count_t *counter){
  //COMMENTING OUT THIS LINE MAKES IT WORK. NORMALLY THERE IS CODE TO SET THE CORRECT VALUE        
  counter-&gt;taken = true;
  }       
  
  //Up counter, respecting saturation
  void sat_count_up(sat_count_t *counter){
          //If counter is saturated
          if ((counter-&gt;count &lt; ( 1 &lt;&lt; counter-&gt;cbits) - 1)) counter-&gt;count = counter-&gt;count + 1;
          sat_count_update(counter);
  }
  
  //Down counter, respecting saturation
  void sat_count_down(sat_count_t *counter){
          //If counter is 0
          if (counter-&gt;count &gt; 0) --counter;
          sat_count_update(counter);
  }
  
  #endif  
  </code></pre>
  
  <p>SaturatingCounterTest.H</p>
  
  <pre><code>#include "SaturatingCounter.H"
  #include "SaturatingCounter.H" //Multiple include to test include guards
  #include &lt;stdbool.h&gt;
  #include &lt;stdio.h&gt;
  
  void main(){
  
          //Initialize counter    
          char i,NULL1, NULL2, bitsA;
          sat_count_t mycounter;
  
          //Test all bit counters
          for(bitsA=1;bitsA&lt;=5;++bitsA){
  
                  sat_count_init(bitsA,&amp;mycounter);
                  printf("***************************** %d bits **************\n",bitsA);
                  printf("**UP**\n");
                  for(i=0;i&lt;((1&lt;&lt;bitsA) + 1);i++) {
                          printf("Counter is currently %d, %sTAKEN.\n",mycounter.count,(!mycounter.taken) ? "NOT " : "");
                          sat_count_up(&amp;mycounter);
                  }
  
  
                  printf("**DOWN**\n");
                  for(i=0; i&lt;(((1&lt;&lt;bitsA) + 1));i++) {
                          printf("Counter is currently %d, %sTAKEN.\n",mycounter.count,(!mycounter.taken) ? "NOT " : "");
                          //THIS IS WHERE bitsA CHANGES!
                          printf("DEBUG BEFORE: BITS: %d\n",bitsA);
  //                      printf ("%p bitsA\n %p mycounter\n",&amp;bitsA, &amp;mycounter);
                          sat_count_down(&amp;mycounter);
                          printf("DEBUG AFTER: BITS: %d\n",bitsA);
  //                      printf ("%p bitsA\n %p mycounter\n",&amp;bitsA, &amp;mycounter);
  
                  }
  
          }
  }     
  </code></pre>