/* Demonstrate more facts about strings. */ #include #include int main( void ) { char s1[4]; char s2[4]; int i; s1[0] = 'a'; s1[1] = 'b'; s1[2] = 'c'; s1[3] = '\0'; s2[0] = 'a';n s2[1] = 'b'; s2[2] = 'c'; s2[3] = ' '; /* Compare memory addresses which, of course, will not be equal. */ if ( s1 == s2 ) { printf("s1 = %s equals s2 = %s\n", s1, s2); } else { printf("s1 = %s does not equal s2 = %s\n", s1, s2); } /* if */ /* Compare string values. However, since s2 is not terminated by '\0', strcmp does not consider them equal. * In fact, printing of s2 is not correct for the same reason. */ if ( strcmp(s1, s2) == 0 ) { printf("s1 = %s equals s2 = %s\n", s1, s2); } else { printf("s1 = %s does not equal s2 = %s\n", s1, s2); } /* if */ /* Now s1 and s2 are identical with regard to the string values. */ s2[3] = '\0'; if ( strcmp(s1, s2) == 0 ) { printf("s1 = %s equals s2 = %s\n", s1, s2); } else { printf("s1 = %s does not equal s2 = %s\n", s1, s2); } /* if */ } /* main */