Why is it that the I seem not be able to use strcmp to compare two strings if I first create an array that just holds enough characters without the \0 character at the end and then put in the \0 at a later step like the following?
// The following from strcmp returns non-zero, so the two strings buff, t1 are
// not equal
char buff[4] = "K7Ag";
buff[4] = '\0';
char t1[5] = "K7Ag";
if(strcmp(buff, t1) == 0) {
printf("they are equal\n");
}
But if I do this:
// strcmp shows they are equal
char buff[5] = "K7Ag";
char t1[5] = "K7Ag";
if(strcmp(buff, t1) == 0) {
printf("they are equal\n");
}
I know I need a null character at the end of each string for strcmp to work properly. So why is it that I first create buff as:
char buff[4] = "K7Ag"
then append "\0" like:
buff[4] = '\0'
then call
strcmp(buff, t1) would not work?
Finally, can I assume that when I create a buff like
char b1[100] = "Abcd"
then I will have 96 null characters (i.e., 96 of those \0) right after the character d in the b1 buffer?
So if I initialize a buffer with less than the allocated size, can I assume the rest of the buffer holds the \0 character?