Is modification of string literals undefined behaviour according to the C89 standard?

Yes, they are non-modifiable in C89.

(C90, 6.1.4) “If the program attempts to modify a string literal of either form, the behavior is undefined”

Even in K&R 2nd edition, there are quotes regarding the immutability of string literals.

(K&R2, 5.5) “the result is undefined if you try to modify the string contents”

(K&R2, Appendix C) “Strings are no longer modifiable, and so may be placed in read-only memory”

In the ANSI C89 Rationale, there is an explanation of why it is non-modifiable:

(ANSI C89 Rationale, 3.1.4) “String literals are specified to be unmodifiable. This specification allows implementations to share copies of strings with identical text, to place string literals in read-only memory, and perform certain optimizations.”

Leave a Comment