retagged by
23,045 views
39 39 votes

 

​​​Consider the following $\mathrm{C}$ program:

#include <stdio.h>
void fX (); 
int main(){
fX();
return 0 }; 
void fX () {  
char a; 
if ((a=g e t c h a r())  ! = '\n')
fX(); 
if (a ! = '\n')
putchar (a); }

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?

  1. The program will not terminate
  2. The program will terminate with no output
  3. The program will terminate with $4321$ as output
  4. The program will terminate with $1234$ as output 

4 Answers

98 98 votes

Answer: option C

11 11 votes

The program reads characters from the input until a newline character ( '${\text\n}$' ) is encountered.

It stores each character in a variable $a$.

If the character is not a newline character, it calls the $fX$ function recursively to read the next character.

Once a newline character is encountered, the recursion stops, and the program starts printing the characters stored in a in reverse order, since the recursion unwinds.

edited by
2 2 votes

Core Concept:
    1. Input is stored in a buffer before getchar() reads it.
    2. getchar() waits for input only if the input buffer is empty.

Let's understand behaviour of getchar() using this pseudo code : 

a = getchar();
b = getchar();
c = getchar();
d = getchar();
e = getchar();
f = getchar();
g = getchar();

Initially our input buffer is empty (as program just started), so at first getchar() program will wait for user for input and suppose user types GATE and presses Enter and so, the buffer becomes : 'G', 'A', 'T', 'E', '\n' 

Now, as we know getchar() reads one character at a time from the input buffer, so a = 'G'
Not only that since input buffer still has 4 characters left, so next 4 encounters of getchar() will not wait for user for input instead they will take it from buffer only and which which results in :
a = 'G'
b = 'A'
c = 'T'
d = 'E'
e = '\n'

and At 6th getchar() program again waits for the user to enter input because now buffer is empty as all characters are consumed. Suppose User types 2026 and presses Enter :
So, the buffer becomes : '2', '0', '2', '6', '\n'
Similarly, f = '2' and g = '0'  and program ends.

so Remaining buffer :  '2', '6', '\n'

These remaining characters stay in the buffer during program execution, but once the program terminates, they are discarded and do not affect any future program executions.

 

This code demonstrates the behaviour of getchar(), showing that it waits for user input only when the input buffer is empty and otherwise reads characters sequentially from the buffer.

#include <stdio.h>
int main() {
    int c;
    int buffer_empty = 1;  // assume empty at start

    for (int i = 1; i <= 7; i++) {
        if (buffer_empty) {
            printf("ENTER: ");
        }
        c = getchar();
        printf("You entered: '%c'\n", c);

        // After '\n' is read, the buffer has been fully consumed.
        if (c == '\n') {
            buffer_empty = 1;
        } else {
            buffer_empty = 0;
        }
    }
    return 0;
}

 



Applying the same concept :

The question states that the input is 1234 followed by a newline, i.e., 1234\n.
So, the input buffer contains: '1', '2', '3', '4', '\n'.

Each call to getchar() reads one character from the buffer.
Thus, the first four calls store '1', '2', '3', and '4' in the variable 'a'.

On the 5th call, getchar() reads '\n', so the recursive calls stop.

As the function returns, putchar() prints the characters in reverse order,
resulting in the output: 4321.

edited by
Answer:
Position:
Show:

Related questions

41 41 votes
6 6 answers
30.3k
30.3k views
Arjun asked Feb 16, 2024
30,286 views
Consider the following $\mathrm{C}$ program:#include <stdio.h int main() { int a=6; int b = 0; while (a<10) { a = a / 12+1 ; a += b ;} printf ("%d", a); return 0 ; }Whi...
54 54 votes
4 answers 4 answers
19.9k
19.9k views
Arjun asked Feb 16, 2024
19,938 views
​​Consider the following $\mathrm{C}$ function definition.int f (int x, int y){ for (int i=0 ; i<y ; i++ ) { x= x + x + y; } return x; }Which of the following statements ...
53 53 votes
5 answers 5 answers
18.6k
18.6k views
Arjun asked Feb 16, 2024
18,569 views
​​​Consider the following $\mathrm{C}$ function definition.int fX(char *a) { char *b = a; while (*b) b++; return b - a; }Which of the following statements is/are TRUE?The...
10 10 votes
1 1 answer
2.8k
2.8k views
GO Classes asked Apr 18, 2022
2,776 views
What will be the output of the following C program?#include<stdio.h void main() { int a =0; int b = (a ? a++: a ? a++ : a ); printf("%d", b); }$0$$-1$$-2$$2$