I believe that in C99, modification of string literals is undefined behaviour. I don't have a copy of that standard but I do have a draft of C1X (n1570) which states in 6.4.5 paragraph 7:
It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
I have found a Stack Overflow question that touches on this topic and contains the following comment from Jonathan Leffler:
Originally, the C89 (C90) standard did not outlaw modifying literals because there was too much code written before the standard that would be broken by it.
But I have also seen lots of discussion of the type of string literals and the fact that they are char[N]
and not const char[N]
. I gather that this decision was taken so that the large body of existing code would not break.
Can anyone give me a definitive answer. Is string literal modification UB in C89?