Is the modification of string literals undefined by the C89 standard?

In C99, I believe that modifying string literals is undefined behavior. I do not have a copy of this standard, but I have a C1X project (n1570), which states in clause 6.4.5 of clause 7:

It is not known whether these arrays are different if their elements have corresponding values. If the program tries to change such an array, the behavior is undefined.

I found the question stack overflow that addresses this topic and contains the following comment by Jonathan Leffler:

Initially, the C89 (C90) standard did not prohibit changing literals because there was too much code written before the standard that would violate it.

But I also discussed a lot of the type of string literals and the fact that they char[N]are not const char[N]. I understand that this decision was made so that most of the existing code is not violated.

Can someone give me a definitive answer. Is the string literary modification of UB in C89?

+5
source share
1 answer

Yes, they do not change in C89.

(C90, 6.1.4) "If a program tries to change a string literal of any form, the behavior is undefined"

Even the K & R 2nd edition has quotes regarding the immutability of string literals.

(K & R2, 5.5) "the result is undefined if you try to change the contents of the string"

(K & R2, Appendix C) "Strings can no longer be modifiable and therefore can be placed in read-only memory"

ANSI C89 , :

(ANSI C89 , 3.1.4) " . , ".

+13

All Articles