On this answer by michael-burr on this question:
what-is-the-type-of-string-literals-in-c-and-c
I found that
In C the type of a string literal is a char[] - it's not const according to the type, but it is undefined behavior to modify the contents
from this I can think that sentence "How are you"
can't be modified (just as char c*="how are you?"
) but once it is used to initialize some char[]
then it can be unless declared as const
.
Apart from this from that answer:
The multibyte character sequence is then used to initialize an array of static storage duration
and from C Primer Plus 6th Edition I found:
Character string constants are placed in the static storage class, which means that if you use a string constant in a function, the string is stored just once and lasts for the duration of the program, even if the function is called several times
But when I tried this code:
#include <stdio.h>
void fun() {
char c[] = "hello";
printf("%s\n", c);
c[2] = 'x';
}
int main(void) {
fun();
fun();
return 0;
}
The array inside function fun
doesn't behave as if it has retained the changed value.
Where am I going wrong on this?