title: Binary to decimal in c
body: <p>I have a simple code to convert binary to decimal numbers. In my compiler, the decomposition works just fine for number less than 1000, beyond the output is always the same 1023. Anybody has an idea ? </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  // how many power of ten is there in a number 
  // (I don't use the pow() function to avoid trouble with floating numbers)
  int residu(int N)
  {
      int i=0;
      while(N&gt;=1){
          N=N/10;
          i++;
      }
      return i;
  }
  
  //exponentiating a number a by a number b
  int power(int a, int b){
      int i;
      int res=1;
      for (i=0;i&lt;b;i++){res=a*res;}
      return res;
  }
  
  //converting a number N
  int main()
  {
      int i;
  
      //the number to convert
      int N;
      scanf("%d",&amp;N);
  
      //the final decimal result
      int res=0;
      //we decompose N by descending powers of 10, and M is the rest
      int M=0;
  
      for(i=0;i&lt;residu(N);i++){
          // simple loop to look if there is a power of (residu(N)-1-i) in N, 
          // if yes we increment the binary decomposition by 
          // power(2,residu(N)-1-i)
          if(M+ power(10,residu(N)-1-i) &lt;= N)
          {
              M = M+power(10,residu(N)-1-i);
              res=power(2,residu(N)-1-i)+res;
          }
      }
      printf("%d\n",res);
  }
  </code></pre>