I've written the following program for adding two binary numbers... When I compile with mingw and visual C under Windows everything works fine. However, when using gcc (version 3.3.3) although it compiles with no errors or warnings i get a very strange result...Try 01010101 and 10101010 for example. How is this possible?
Thanks...
CODE
#include <stdio.h>
#include <stdlib.h>
#define N 8
int main()
{
int A[N], B[N], SUM[N+1];
int i, c = 0;
char ch;
printf("Enter 1st binary number (%d digits):", N);
for(i=0; i<N; i++) {
ch = getchar();
A[i] = ch - '0';
}
fflush(stdin);
printf("\n\nEnter 2nd binary number (%d digits):", N);
for(i=0; i<N; i++) {
ch = getchar();
B[i] = ch - '0';
}
for(i=N-1; i>=0; i--)
{
if(c==0) {
if((A[i]==0 && B[i]==1) || (A[i]==1 && B[i]==0) ) { SUM[i+1] = 1;}
if(A[i]==1 && B[i]==1) { SUM[i+1] = 0; c = 1;}
if(A[i]==0 && B[i]==0) { SUM[i+1] = 0;}
}
else {
if((A[i]==0 && B[i]==1) || (A[i]==1 && B[i]==0)) {SUM[i+1] = 0;}
if(A[i]==1 && B[i]==1) {SUM[i+1]=1; c=1;}
if(A[i]==0 && B[i]==0) {SUM[i+1] = 1; c=0;}
}
}
if(c==1) SUM[0] = 1;
else SUM[0] = 0;
printf("\n\nThe sum is: ");
for(i=0; i<N+1; i++) printf("%d", SUM[i]);
printf("\n\n");
return 0;
}