What will be the output when the code snippet given below is executed?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
What will be the output when the code snippet given below is executed?
char *sentence = "Changing Experience";
printf("%d", printf("%s", sentence));
Show answer & explanation
Answer: D. Changing Experience19
The inner printf("%s", sentence) prints "Changing Experience" (19 characters) and returns 19. The outer printf("%d", ...) then prints this return value 19. Combined output is "Changing Experience19".
Step-by-step Derivation:
Inner printf("%s", sentence) executes first:
- Prints: "Changing Experience"
- Returns: number of characters printed = 19 ("Changing Experience" has 19 characters: C-h-a-n-g-i-n-g-space-E-x-p-e-r-i-e-n-c-e)
Outer printf("%d", 19) executes with the return value:
- Prints: "19"
- Returns: number of characters printed = 2
Total console output: "Changing Experience" + "19" = "Changing Experience19"