C strings and strcat on string literals (Turbo C, 16-bit DOS): what will the following program print or do? #include<stdio.h> #include<string.h> int main() { char *str1 = 'India'; char *str2 = 'CURIOUSTAB'; char *str3; str3 = strcat(str1, str2); // concatenating into a string literal printf('%s %s ', str3, str1); return 0; } Assume a Turbo C compiler on a 16-bit DOS platform, as originally intended.

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:

  • str1 and str2 are pointers to string literals ('India' and 'CURIOUSTAB').
  • strcat(dest, src) writes into dest’s buffer, appending src and the terminating '\0'.
  • String literals are not modifiable; attempting to write to them produces undefined behavior (often a run-time crash or immediate error).


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:

Identify dest: dest == str1 → points to a literal → not writable.strcat will try to find '\0' at the end of 'India' and copy bytes from 'CURIOUSTAB' into the memory where 'India' resides.This write into read-only storage triggers undefined behavior → practically an error/crash.


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:

CuriousTab India / CuriousTab CuriousTab / India India / India CURIOUSTAB: All assume a successful, defined concatenation. That would require a writable array as destination, which we do not have here.


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.

More Questions from Strings

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion