title: not using strcmp properly in C
body: <p>I wrote this method to find the biggest power of 2 that will fit in a given decimal. The decimal is in char array format to avoid input overflow error with number storage. Powers of 2 are calculated with pow(2, power) format of float  ie. 8.000000 This number is then sent to a method to remove the period and the 0's that trail. ie. 8.000000 turns into 8</p>
  
  <pre><code>1  #include &lt;string.h&gt;
  2  #include &lt;stdio.h&gt;
  3  #include &lt;stdlib.h&gt;
  4  #include &lt;memory.h&gt;
  5  #include &lt;math.h&gt;
  6
  7   int i;
  16
  17  void removeFloatZeros(char *floatvalue)
  18  {
  19      char *ptr = strchr(floatvalue, '.');
  20      *ptr = '\0';
  21  }
  22
  45
  173 char *decimalToBinary(char *decimal)
  174 {
  176     int x;
  177     double power = 0;
  178     char *binary = malloc(sizeof(char *) * 1024);
  179     char *twosPower = malloc(sizeof(char *) * 1024);
  180
  181     /* What is the greatest power of 2 that will fit into the decimal? */
  182     for(x = 0; x &lt;= 30; x++)
  183     {
  184         power = pow(2.0, x);
  185         snprintf(twosPower, 1023, "%f", power);
  186         removeFloatZeros(twosPower);
  189         printf("strcmp(decimal, twosPower) = %d\n", strcmp(twosPower, decimal));  
  190         memset(twosPower, '\0', 1023);
  191     }
  214 }
  215
  216 int main(int argc, char*argv[])
  217 {
  218     char *dec1 = argv[1];
  219     decimalToBinary(dec1);
  220     return 1;
  221 }
  222
  </code></pre>
  
  <p>For example if I input 20 into argv[1] it will output:</p>
  
  <pre><code>strcmp(decimal, twosPower) = -1
  strcmp(decimal, twosPower) = -1  
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = -1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = -1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = -1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = -1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = -1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = -1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = -1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = -1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = 1
  strcmp(decimal, twosPower) = -1
  </code></pre>
  
  <p>Where am I going wrong with this? Also, ignore the ending condition for the for loop. It's supposed to output all 1's before the 6th iteration and all -1's on the 6th and after the 6th iteration.</p>