ACtE04 · Chapter 4Computer Organization & Embedded Systems🏛️ Past Licensure QuestionID: 6a73c147
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. Attempting to modify a string literal through a pointer (e.g., char *s = "Hello"; s[0] = 'h';) leads to undefined behavior. Many modern compilers will flag this as a compile-time error or at least a warning, especially with strict type checking where string literals are treated as const char[].
Option Analysis:
•
Option A (Compile time error): ✅ Correct. The compiler detects the attempt to modify a read-only string literal and reports an error.
•
Option B (Hello World! 34): Would require successful program execution, which won't happen due to the compilation error.
•
Option C (Hello World! 10000): Would require successful program execution.
•
Option D (Hello World! followed by a junk value): Would require partial execution, which won't occur if compilation fails.
Reference: Mock Test 01NEC Licensure Syllabus