What is the output of a C program that attempts to modify a string literal?
What is the output of a C program that attempts to modify a string literal?
Multiple Choice Options
- A✓ Correct Option
Compile time error
- B
Hello World! 34
- C
Hello World! 10000
- D
Hello World! followed by a junk value
Detailed Technical Solution & Explanation
In C, string literals (e.g., "Hello World!") are stored in read-only memory segments. Attempting to modify a string literal (e.g., char *str = "Hello"; str[0] = 'h';) results in undefined behavior. Depending on the compiler and its settings, this typically manifests as a compile-time error (with strict type checking) or a runtime segmentation fault.
Given the options, the expected answer is a compile-time error, which occurs when the compiler detects an attempt to write to a const-qualified or read-only string literal.