In Turbo C (16-bit), what happens when you assign a string literal to a single char element? #include<stdio.h> int main() { char str[10] = "India"; str[6] = "CURIOUSTAB"; /* invalid: assigning char* to char / printf("%s ", str); return 0; }

Difficulty: Easy

Correct Answer: Error

Explanation:


Introduction / Context:
This item checks type correctness in C. The code attempts to assign a string literal (type char) to one element of a char array (type char). That is a type mismatch at compile time.



Given Data / Assumptions:

  • Compiler context: Turbo C (16-bit) is specified, but standard C typing rules still apply.
  • str is char[10] initialized to "India".
  • str[6] is a single char lvalue.
  • The right-hand side is "CURIOUSTAB", a char* to static storage.


Concept / Approach:
In C, assignment requires compatible types. A char cannot receive a pointer value. Additionally, str[6] is beyond the current string's terminator (indexing 0..4 for "India", index 5 is '\0'), but even aside from bounds, the type mismatch alone is a compile-time error.



Step-by-Step Solution:
Parse the assignment: str[6] (char) = "CURIOUSTAB" (char*).The types are incompatible → diagnostic issued by the compiler.The program does not compile successfully in standard C or Turbo C.


Verification / Alternative check:
To copy text into the array, use strcpy(&str[6], "CURIOUSTAB") provided there is enough space in str (which there is not here). Or assign a single character, e.g., str[6] = 'X'.



Why Other Options Are Wrong:
(a), (b), (c) imply successful compilation and runtime behavior. Given the type error, no such output occurs. (e) presumes runtime but compilation fails first.



Common Pitfalls:
Confusing characters with strings; forgetting that string literals denote pointers to char (array-to-pointer decay).



Final Answer:
Error

More Questions from Strings

Discussion & Comments

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