-
[Help] Strings in C
Code:
#include <stdio.h>
int main() {
char str3[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
char str4[ ] = {'h', 'e', 'l', 'l', 'o'};
printf("%s, %s", str3, str4);
return 0;
}
The output to this program is hello, hellohello
why does it print that instead of hello, hello ?
-
Because str4 doesn't have a \0 to mark the end of the string.
So printf just keeps running through memory until it finds a \0.
In your case, that just happened to be your str3.
But it could have just as easily printed any random memory contents until it found a \0 (or crashed).