GATE CS 2024 Set 1 — Question 19
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.MCQ+1 / -0.33MediumFunctions & RecursionC ProgrammingProgramming & Data Structures
Programming & Data Structures → C Programming → Functions & Recursion
Last updated
Question
Consider the following C program:Assume that the input to the program from the command line is 1234 followed by
a newline character. Which one of the following statements is CORRECT?
#include <stdio.h>
void fX();
int main(){
fX();
return 0;}
void fX(){
char a;
if((a=getchar()) != '\n')
fX();
if(a != '\n')
putchar(a);}
a newline character. Which one of the following statements is CORRECT?
Correct answer
(C) The program will terminate with 4321 as output
Solution
The function
fX() is a recursive function that reads characters from the standard input one by one.1.Base Case: The recursion stops when
getchar() reads a newline character '\n'. At this point, the if((a=getchar()) != '\n') condition fails, and the function starts returning.2.Recursive Step: For any character other than
'\n', the function calls itself recursively: fX(). This pushes the current character a onto the function call stack.3.Post-recursion Action: After the recursive call returns, the function executes
Because the printing happens after the recursive call returns, the characters are printed in the reverse order of their arrival (Last-In, First-Out behavior).Execution Trace for input "1234\n":if(a != '\n') putchar(a);. This prints the character that was read before the recursive call.fX()(1st call) reads '1', callsfX()(2nd call).fX()(2nd call) reads '2', callsfX()(3rd call).fX()(3rd call) reads '3', callsfX()(4th call).fX()(4th call) reads '4', callsfX()(5th call).fX()(5th call) reads '\n', the condition('\n' != '\n')is false. It skips the recursive call and theputchar(sincea == '\n'), then returns to the 4th call.fX()(4th call) resumes,ais '4',putchar('4')prints 4, returns to 3rd call.fX()(3rd call) resumes,ais '3',putchar('3')prints 3, returns to 2nd call.fX()(2nd call) resumes,ais '2',putchar('2')prints 2, returns to 1st call.fX()(1st call) resumes,ais '1',putchar('1')prints 1, returns tomain().
Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on C Programming
2026 Set 2 Q12The set T represents various traversals over binary tree. The set S represents the order of…2026 Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] **P3**…2026 Set 1 Q23Let be an odd number greater than 100. Consider a binary minheap with elements stored in an…2026 Set 1 Q24Consider a hash table that is initially empty. The hash table is maintained…2026 Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]