Difficulty: Medium
Correct Answer: Error
Explanation:
Introduction / Context:
This question checks your understanding of C string storage, mutability of string literals, and the behavior of strcat when given an invalid destination. We are asked about the observable outcome under a classic Turbo C (16-bit DOS) environment, which stores string literals in read-only segments or otherwise non-writable memory.
Given Data / Assumptions:
Concept / Approach:
strcat requires dest to point to a writable array with enough free space to hold the concatenation. Here, dest is str1, which points to a literal. Writing into a literal violates the C standard’s constraint and results in undefined behavior. Historically, on Turbo C and similar compilers, this typically causes an error or crash rather than clean output.
Step-by-Step Solution:
Verification / Alternative check:
Replace str1 with a char array of sufficient size (e.g., char str1[32] = 'India';). Then strcat succeeds and prints 'IndiaCURIOUSTAB IndiaCURIOUSTAB'. The failure only occurs when dest is a literal.
Why Other Options Are Wrong:
Common Pitfalls:
Treating 'char *p = 'text';' as if it were a writable array; forgetting that strcat needs adequate writable space in the destination.
Final Answer:
Error.
Discussion & Comments