title: Decimal to Binary Program
body: <p>I am working on an assignment for a C Programming course in regards to converting a decimal to binary using a function that takes in an unsigned char as its input and has a void output. The function will print the binary code of the unsigned char. A hint for the assignment is to create an array of exponents starting with 128 and going down to 1. </p>
  
  <p>I started working on the assignment and ran the debugger, but my program is not working and I am getting a run time error message: Run-Time Check Failure #2 - Stack around the variable <code>userInput</code> was corrupted.</p>
  
  <p>I would appreciate some suggestions on how I can fix my code and if there is a much simple way to write it in order to make the code easier to understand.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  unsigned char DecimalToBinary(unsigned char decimalInput);
  
  void main() {
  
      unsigned char userInput = ' ';
      unsigned char resultOfUserInput = DecimalToBinary(userInput);
  
      printf("Enter a number less than 256: ");
      scanf_s("%u", &amp;userInput);
  
  
      printf("%u in binary: %u", userInput, resultOfUserInput);
  
  
      system("pause");
  }
  
  unsigned char DecimalToBinary(unsigned char decimalNumber) {
  
      int arrayOfExponents[128] = {}, i = 1, j;
  
      while (decimalNumber &gt; 0) {
  
          arrayOfExponents[i] = decimalNumber % 2;
          i++;
          decimalNumber = decimalNumber / 2;
      }
  
      for (j = i - 1; j &gt; 0; j--) {
          printf("%i", arrayOfExponents[j]);
      }
      return 0;
  }
  </code></pre>