This C program compares two distinct arrays and if the arrays are same then content results in FALSE. The behavior of comparison is explained when we note that the comparison is a comparison of addresses, not contents.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include < stdio.h > int main(void) { int a[2] = { 1, 2 }; /* The aggregates like {1,2} are literals for arrays */ int b[2] = { 2, 3 }; int i; /* It is legal to use subscripts on arrays, both on the left and on the right hand side of assignments. */ for (i = 0; i < 2; i++) { a[i] = b[i]; } /* It is not legal to assign arrays, like in a=b; */ /* The comparison of two distinct arrays with the same content * results in FALSE. So below we print "They are not equal" */ if (a == b) { printf("They are equal\n"); } else { printf("They are not equal\n"); } /* The following comparison results in TRUE. */ if (a == a) { printf("Of course a is equal to a\n"); } else { printf("No, a is not equal to a\n"); } /* The behavior of comparison is explained when we note that the * comparison is a comparison of addresses, not contents. */ /* We cannot print out an array as a single unit. We have to print out * its elements one at a time. */ for (i = 0; i < 2; i++) { printf("a[%1d] = %3d\n", i, a[i]); } } |