title: Adding two integers (range can be greater than long long int) in C
body: <p>I wrote the following code to add two integers greater than long long int but somehow it seems that it is entering in an infinite loop. </p>
  
  <p>I am only moderately acquainted with the language so the code is a bit clumsy. It will be great if someone points out my mistake.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;string.h&gt;
  int main()
  {
      char a[127], c[127];
      char b[127], d[127];
      char result[127];
      int i, j, k, l1, l2, sum, carry;
      printf("\n\nenter 1st number\n\n");
      gets(a);
      printf("\n\nenter 2nd number\n\n");
      gets(b);
      l1 = strlen(a);
      l2 = strlen(b);
      //  for(i=0;i&lt;=l1;i++)
      //      printf("%c",a[i]);
      j = l1;
      k = l2;
      /*for(i=l1;i&gt;=0;i--)
      {
      c[j]=a[i];
      j--;
      }
      for(i=l2;i&gt;=0;i--)
      {
      d[j]=b[i];
      j--;
      }*/
      printf("\n%d %d\n", j, k);
      if (l1&gt;l2)
      {
          for (i = l1; i &lt;= l2; i--)
          {
              c[i] = a[j];
              d[i] = b[k];
              j--;
              k--;
          }
          for (i = l2; i &lt;= 0; i--)
          {
              c[i] = a[j];
              d[i] = 0;
              j--;
          }
  
      }
      //  printf("hello");
      j = l1;
      k = l2;
      if (l1&lt;l2)
      {
          for (i = l2; i &lt;= l1; i--)
          {
              c[i] = a[j];
              d[i] = b[k];
              j--;
              k--;
          }
          for (i = l1; i &lt;= 0; i--)
          {
              c[i] = 0;
              d[i] = b[k];
              k--;
          }
      }
      j = l1;
      k = l2;
      if (l1 = l2)
      {
          for (i = l2; i &lt;= 0; i--)
          {
              c[i] = a[j];
              d[i] = b[k];
              j--;
              k--;
          }
      }
      if (l1&gt;l2)
      {
          for (i = l1; i &lt;= 0; i--)
              printf("%c", c[i]);
      }
  
      if (l1&gt;l2)
      {
          for (i = l1; i &gt;= 0; i--)
          {
              if (i = l1)
                  carry = 0;
              else
                  /*if(i=l2)
                  carry[i]=0;
                  else
                  carry[i]=sum[i]*/
                  carry = sum / 10;
              sum = ((c[i] - '0') + (d[i] - '0')) + carry;
              //  if(i!=0)
              result[i] = sum % 10 + '0';
              //  else
              //      result[i]=sum[i];
          }
          //  for(i=l1-l2;i&lt;=l1;i++)
          //      result[i]=c[i];
      }
      if (l1&lt;l2)
      {
          for (i = l2; i &gt;= 0; i--)
          {
              if (i = l2)
                  carry = 0;
              else
                  carry = sum / 10;
              sum = (c[i] - '0') + (d[i] - '0') + carry;
              //if(i!=0)
              result[i] = sum % 10 + '0';
              //  else
              //      result[i]=sum[i];
          }
          //      for(i=l2-l1;i&lt;=l2;i++)
          //          result[i]=d[i];
      }
      if (l1 = l2)
      {
          for (i = l1; i &gt;= 0; i--)
          {
              if (i = l1)
                  carry = 0;
              else
                  carry = sum / 10;
              sum = (c[i] - '0') + (d[i] - '0') + carry;
              if (i != 0)
                  result[i] = sum % 10 + '0';
              else
                  result[i] = sum + '0';
          }
      }
      printf("\n\nthe result is\n\n");
      if (l1 &gt;= l2)
      {
          for (i = 0; i &lt;= l1; i++)
              printf("%c", result[i]);
      }
      else
      {
          for (i = 0; i &lt;= l2; i++)
              printf("%c", result[i]);
      }
      printf("\n\n");
      return 0;
  }
  </code></pre>