Type mismatch: attempting to store a string literal in a single char variable. What happens? #include<stdio.h> int main() { char str = 'CuriousTab'; // invalid: char cannot hold a whole string printf('%s ', str); return 0; }

Difficulty: Easy

Correct Answer: Error

Explanation:

Introduction / Context:This problem highlights type correctness in C: a string literal represents an array of characters, while a single char variable stores only one character. It also checks awareness that %s expects a char* (pointer to char), not a char value.

Given Data / Assumptions:

  • Declaration uses char str, not char* or char array.
  • Assignment tries to put a multi-character literal into a single char.
  • printf with %s expects a char*; passing a char value results in a type mismatch.

Concept / Approach:char str = 'CuriousTab' is ill-formed; in standard C, multi-character constants like 'AB' are integer constants, but string literals use double quotes. Here double quotes are used, so assigning a string literal to char is a compilation error. Even if it compiled erroneously, printf('%s', str) would require a pointer, not a char, causing undefined behavior. Proper code would be either char str[] = 'CuriousTab'; or char str = 'CuriousTab';

Step-by-Step Solution:

Compilation stage: detect type mismatch (char vs. char[...]).printf usage: %s needs char, not char.Therefore the code fails to compile correctly; treat as an error.

Verification / Alternative check:Change declaration to char str[] = 'CuriousTab'; and the program will print the word as expected.

Why Other Options Are Wrong:

Printing 'CuriousTab' or base address presumes a valid pointer and a correct format specifier, which we do not have.'No output': compilation fails before runtime.'Program prints a single C': not applicable to this code as written.

Common Pitfalls:Confusing single characters (char) with strings (arrays/pointers to char) and mismatching printf format specifiers.

Final Answer:Error.

More Questions from Strings

Discussion & Comments

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