What compiler says "something wrong"?
One says:
In function 'int main()':
10:20: error: 'x' was not declared in this scope
10:23: error: 'y' was not declared in this scope
10:26: error: 'z' was not declared in this scope |
Indeed, the x, y, and z are not characters, they are identifiers. Names of variables, but you have not declared such variables.
Were you trying to initialize the array with actual characters? If yes, you should use literal constants:
char abc[2] {'x', 'y', 'z'};
That, however, reveals a new problem. You state that the array has
2 elements. Yet, you try to store
3 values in it. Not possible, or as a compiler could say:
In function 'int main()':
10:33: error: too many initializers for 'char [2]' |
We don't need to write the size for the array, because the compiler can calculate it from the number of initializers.
However, you do have yet another problem on line 12. The output operator does effectively get a memory address of the first element of the array and assumes that the array contains a C-style string. A C-style string has a null character marking the end of the string. Thus, the output reads consecutive characters from memory and prints them, until it encounters a null.
You don't store any null into the array and therefore the output will continue to read beyond the memory reserved for the array. That is undefined behaviour.