C printf with a string format specifier: what is printed? #include<stdio.h> int main() { char str; str = "%s"; printf(str, "K "); return 0; }

Difficulty: Easy

Correct Answer: K

Explanation:


Introduction / Context:
printf uses a format string to control how subsequent arguments are printed. With the "%s" format, printf expects a pointer to a null-terminated string. This question checks whether you can trace a simple printf call passing a one-character string with a newline.


Given Data / Assumptions:

  • Format string: "%s".
  • Argument: the string literal "K\n" (letter K then newline then null terminator).


Concept / Approach:
The "%s" format prints characters from the provided char until the terminating '\0'. Therefore, the two characters 'K' and '\n' are emitted. Many MCQs capture the visible part (“K”) and ignore that a newline follows; here the important visual output is the letter K followed by a line break.


Step-by-Step Solution:

printf("%s", "K\n")Output sequence → 'K' then newline.On a terminal, you see K and the cursor moves to the next line.


Verification / Alternative check:
Change the literal to "K" (no newline) to see the cursor remain on the same line.


Why Other Options Are Wrong:

Error/No output: printf is called correctly with a matching argument type.%s: That would print the percent-escaped letters literally, which does not happen here.K%s: Not produced by printf for this call.


Common Pitfalls:
Forgetting the newline is part of the literal; misreading the format string as a literal to be printed rather than a directive.


Final Answer:
K.

More Questions from Pointers

Discussion & Comments

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