/* COMP2401/2001 - W13 Be careful when initializing arrays of chars (strings) Remember: Every string is an array of chars (which is just a pointer to a char). But, every char* (array of chars) is not a string. Strings are arrays of chars that end with the '\0' NULL character. mjh - 2013 */ /* run the code with the local comments in place and also commented out. Make sure you know why there is a difference in the output. */ #include // this initialization does NOT automatically add a '\0' to the // end of the array char b[] = {'t', 'w', 'o', ' ', 'f', 'i', 's', 'h'}; // this initialization DOES automatically add the '\0' to the end char a[] = "one fish"; char c[5] = "abc"; int main(){ // initializing an array with at least 1 less char than the array // was created for will automatically fill the remaining chars with // zeros. (and zero is ASCII value for '\0', which is NULL) const int size = 100; char b[size+1] = {'t', 'w', 'o', ' ', 'f', 'i', 's', 'h'}; char a[] = "one fish"; char c[5] = "abc"; printf("string a = \"%s\"\n", a); printf("string b = \"%s\"\n", b); }