This is a program that I wrote to add two numbers together that are too large for the C language to store. It stores the numbers as strings instead and processes one digit at a time. But I suspect that the program could be done in fewer steps. In what ways can I reduce the size of my program? Where am I taking more steps than necessary?
#include <stdio.h>
#include <string.h>
int main()
{
char *a = "1490438498359508";
char *b = "1575390843059845";
char *sum = "";
char str[15] = "";
char src[512];
int carry = 0;
int alength = strlen(a);
int blength = strlen(b);
int n;
int i;
int c, d;
for (n=1; n<blength+1; n++) {
int c=0, d=0;
i = 0;
c = a[alength-n]-48;
d = b[blength-n]-48;
i = c+d;
if (carry == 1) {
i=i+1;
}
if (i > 9) {
i = i-10;
carry=1;
} else {
carry=0;
}
int aInt = i;
char str[15] = "";
sprintf(str, "%d", aInt);
strcat(src, str);
}
int nlength = strlen(src);
int x;
for (x=0; x<nlength+1; x++) {
printf("%c", src[nlength-x]);
}
printf("\n");
return 0;
}