fork download
  1. # define max 1000
  2. char arr[max];
  3. void subtract(char *a1,char *b1)
  4. {
  5. int l1,l2;
  6. char temp[max],a[max],b[max];
  7. int c=0,sub=0,m=0;
  8. int i,j,k=0;
  9.  
  10. for(i=strlen(a1)-1;i>=0;i--)
  11. a[k++]=a1[i];
  12.  
  13. a[k]='\0';k=0;
  14. for(i=strlen(b1)-1;i>=0;i--)
  15. b[k++]=b1[i];
  16. b[k]='\0';
  17.  
  18. //printf("%s %s\n",a,b);
  19. l1=strlen(a);
  20. l2=strlen(b);
  21. if(l2>l1)
  22. {
  23. strcpy(temp,b);
  24. strcpy(b,a);
  25. strcpy(a,temp);
  26. }
  27. l1=strlen(a);
  28. l2=strlen(b);
  29. k=0;
  30. for(i=0;i<l2;i++)
  31. {
  32. int n1=a[i]-'0';
  33. int n2=b[i]-'0';
  34. printf("n1=%d n2=%d ",n1,n2);
  35. sub=n1-n2-c;
  36. if(sub<0)
  37. {
  38. sub=10-sub;c=1;
  39. arr[k++]=sub+'0';
  40. printf("%d %c\n",sub,arr[k-1]);
  41.  
  42. }
  43. else {c=0;arr[k++]=sub+'0'; printf("%d %c\n",sub,arr[k-1]);}
  44. }
  45.  
  46. for(i=l2;i<l1;i++)
  47. {
  48. int n1=a[i]-'0';
  49. sub=n1-c;
  50. if(sub<0)
  51. {
  52. sub=10-sub;c=1;
  53. arr[k++]=sub+'0';
  54. printf("2== %d %c\n",sub,arr[k-1]);}
  55. }
  56. else {c=0;arr[k++]=sub+'0'; printf("2== %d %c\n",sub,arr[k-1]);}}
  57. }
  58. arr[k]='\0';
  59.  
  60. printf("arr=%s\n",arr);
  61.  
  62. for(i=strlen(arr)-1;i>=0;i--)
  63. printf("%c",arr[i]);
  64.  
  65. return arr;
  66. }
  67.  
  68.  
  69. int main()
  70. {
  71. char a[max],b[max],c[max];
  72. scanf("%s %s",a,b);
  73. subtract(a,b);
  74. return 0;
  75. }
  76.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
9999 123
compilation info
prog.c: In function ‘subtract’:
prog.c:10: warning: implicit declaration of function ‘strlen’
prog.c:10: warning: incompatible implicit declaration of built-in function ‘strlen’
prog.c:23: warning: implicit declaration of function ‘strcpy’
prog.c:23: warning: incompatible implicit declaration of built-in function ‘strcpy’
prog.c:34: warning: implicit declaration of function ‘printf’
prog.c:34: warning: incompatible implicit declaration of built-in function ‘printf’
prog.c:54: warning: incompatible implicit declaration of built-in function ‘printf’
prog.c:56: error: ‘else’ without a previous ‘if’
prog.c:56: warning: incompatible implicit declaration of built-in function ‘printf’
prog.c:8: warning: unused variable ‘j’
prog.c:7: warning: unused variable ‘m’
prog.c: At top level:
prog.c:57: error: expected identifier or ‘(’ before ‘}’ token
prog.c:58: error: ‘k’ undeclared here (not in a function)
prog.c:58: warning: data definition has no type or storage class
prog.c:58: warning: type defaults to ‘int’ in declaration of ‘arr’
prog.c:60: error: expected declaration specifiers or ‘...’ before string constant
prog.c:60: error: expected declaration specifiers or ‘...’ before ‘arr’
prog.c:60: warning: data definition has no type or storage class
prog.c:60: warning: type defaults to ‘int’ in declaration of ‘printf’
prog.c:60: warning: conflicting types for built-in function ‘printf’
prog.c:62: error: expected identifier or ‘(’ before ‘for’
prog.c:62: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘>=’ token
prog.c:62: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘--’ token
prog.c:65: error: expected identifier or ‘(’ before ‘return’
prog.c:66: error: expected identifier or ‘(’ before ‘}’ token
prog.c: In function ‘main’:
prog.c:72: warning: implicit declaration of function ‘scanf’
prog.c:72: warning: incompatible implicit declaration of built-in function ‘scanf’
prog.c:71: warning: unused variable ‘c’
stdout
Standard output is empty