[Solved] Can a string literal and a non-string non-compound literal be modified? [duplicate]


From the C Standard (6.4.5 String literals)

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.

As for your statement.

The second paragraph says that “C does not strictly prohibit modifying
string literals” while compilers do. So should a string literal be
modified?

Then compilers do not modify string literals. They may store identical string literals as one array.

As @o11c pointed out in a comment in the Annex J (informative) Portability issues there is written

J.5 Common extensions

1 The following extensions are widely used in
many systems, but are not portable to all implementations. The
inclusion of any extension that may cause a strictly conforming
program to become invalid renders an implementation nonconforming.
Examples of such extensions are new keywords, extra library functions
declared in standard headers, or predefined macros with names that do
not begin with an underscore.

J.5.5 Writable string literals

1 String literals are modifiable (in which case, identical string
literals should denote distinct objects) (6.4.5).

6

solved Can a string literal and a non-string non-compound literal be modified? [duplicate]