I couldn't exactly explain my question well in the title. Here are two sets of code:
int main() {
char str[81] = "Hello World";
scanf("%s", str);
str[3] = 'X';
printf("%s", str);
return 0;
}
When entering 789, the output is "789Xo World", which shows that the scanf function is replacing the first 3 indices of the char array with the input and the str[3] line replaces the 4th with 'X'. This makes sense to me.
int main() {
char str[81] = "Hello World";
scanf("%s", str);
printf("%s", str);
return 0;
}
When inputting 789, this outputs 789. Scanf is no longer replacing the first 3 indices of str, but rather the whole thing. It confuses me that manually changing the 4th index (str[3]) would somehow make the program change the front of str instead of replacing it entirely?
I'm very new to C and I can't find an explanation anywhere in my textbook. Thanks ahead of time for your help.