title: Result Decremented by 1 during a conversion between string to integer (and only with specific values of string length)
body: <p>I'm writing a program in C that converts a string of n characters into an integer only if this string's first character is the minus symbol. The program is the following:</p>
  
  <pre><code>#include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  
  int StringToInt(char *string){
      int i=0;
      int integer=0;
      int length = strlen(string);
      if (string[0]=='-'){
          for (i=1; i&lt;length; i++){
          integer += (string[i]-48)*pow(10, (length-1-i));
          }return (integer*(-1));
      }else{
          return 0;
      }
  }
  </code></pre>
  
  <p>My program works fine when I call the function above with a string with an odd value of string length, while otherwise it returns the right value but decremented by one. Now I'll make it clearer with an example: suppose I create a string called "Number" and I assign to this the number "-234". If we put this string in the function above, the string's length will be calculated as 4. The function in this case won't return -234, but -233. If, instead of -234, I had assigned to the string a number like "-1456", the function would have returned the correct number.</p>