in Programming in C edited by
2,676 views
1 vote
1 vote

GATE CSE 2024 | Set 1 | Question-9

  1. GATE CSE 2024 | Set 1 | Question-45
  2. GATE CSE 2024 | Set 1 | Question-45
  3. GATE CSE 2024 | Set 1 | Question-45
  4. GATE CSE 2024 | Set 1 | Question-45

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

#include <stdio.h>  void fX () {

void fX () ;  char a;

int main() { if ((a=g e t c h a r())  ! = '\n')

fX}() ; fX();

return 0 ; 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 
in Programming in C edited by
by
2.7k views

3 Answers

4 votes
4 votes

Answer: option C

0 votes
0 votes

The question is vague. They said they have input as 1234 but how this input is being provided?

Inside main there is no input available to support the string entered.

In the function definition itself:

a-> If the input is passed in the parameter then char a declaration is invalid. 

b-> If the input is passed in the char a itself then we can't simply pass the value as 1234 with the given declaration. Because 1234 will be a string. But the given program simply declares a character variable a. All values needs to be entered one by one. But again no looping mechanism is given. 

Now considering the fact that the input 1234 is given one by one then for the given input we can only pass 1 as the input as after that the program will terminate and in doing so we will get "no output in the answer". Hence option B.

Here's an image of what points I am trying to make: 

and if the values are provided one by one.

We are getting a blank screen as an output.

Please note that this answer is limited only up to my understandings. Please correct me if there's something wrong. 

4 Comments

I would like to tell you that getchar() is used to take input character from the input stream and the putchar() is used to output the answer. Here, answer will be option C simply. where it will be taking input characters till the newline character (enter key is pressed).
And later it will backtrack the function from the last character it received.

For the given character sequence 1234, it will result as 4321.


Hope you got it and now its clear to you!!!!

0
0

I know getchar() and putchar(), but how the input is being given to "char a" Please provide if possible the working code. Because providing 1234 to the char variable will not simply work as char only accepts a single valued variable. 

0
0



I am trying to upload a screenshot of image but I guess its not working. Hope it works this time.

0
0

Are you sure of using <bits/stdc++.h> ?

And how you are giving your output screen if the code is not even taking any inputs. 

Okay let's leave all things aside.

Please tell me in your character type variable "a" how you are giving the input as '1234'. 

0
0
0 votes
0 votes

The program reads characters from the input until a newline character ('\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.

Answer:

Related questions