char ch[2];
ch[0] = 'h';
ch[1] = '\0';
or can i just do
char ch[1];
ch[0] = 'h';
in which ‘\0’ would already be implied?
The reason why i’m doing it this way is because my program has a loop in it in which i would constantly be changing this character and concatenating it to a longer string.
Assigning just
ch[0]will not assign zero to thech[1]. You can initialize both characters in the array with a string literal in a single line, like this:This will put
'h'intoch[0], and a terminating zero intoch[1].