C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 6)
6.
What will be the output of the program If characters 'a', 'b' and 'c' enter are supplied as input?
#include<stdio.h>
int main()
{
void fun();
fun();
printf("\n");
return 0;
}
void fun()
{
char c;
if((c = getchar())!= '\n')
fun();
printf("%c", c);
}
Answer: Option
Explanation:
Step 1: void fun(); This is the prototype for the function fun().
Step 2: fun(); The function fun() is called here.
The function fun() gets a character input and the input is terminated by an enter key(New line character). It prints the given character in the reverse order.
The given input characters are "abc"
Output: cba
Discussion:
31 comments Page 1 of 4.
Vinnu said:
3 years ago
Why it print only 3 characters? Please expxlain.
Faizan said:
4 years ago
Since it is recursion output is printed in LIFO manner.
Ruku said:
7 years ago
Hello all.
Everyone recalled the program of factorial 5 = 5 * 4 * 3 * 2 * 1.
Here what is recursion is a function called its self.
Similarly in this program fun() function calling itself.
Therefore output is in reverse order.
Everyone recalled the program of factorial 5 = 5 * 4 * 3 * 2 * 1.
Here what is recursion is a function called its self.
Similarly in this program fun() function calling itself.
Therefore output is in reverse order.
(1)
Laxmi said:
7 years ago
%c prints only single char. Then how it prints cba. Please explain me how the program executes?
Shubham said:
8 years ago
Fun() function declared in main function. How it is accessible by fun() Only main function can call it. Correct me if I am wrong.
Raman_Kaur said:
8 years ago
How can we identify that it will take only 3 characters?
Ishvarya said:
8 years ago
I am not getting this, Anyone please explain it step by step.
Ravi Mule said:
9 years ago
@Vasuveer Singh.
You are right, it's a recursion program.
You are right, it's a recursion program.
Chandu said:
9 years ago
In the recursive call every time stack allocates a memory and store the values, here on the failure of condition pointer must be go back to main so at this time it prints reversely.
(2)
BISWAJIT MOHAPATRA said:
9 years ago
Not understanding, Can anyone explain it to me?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers