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 2 of 4.
BISWAJIT MOHAPATRA said:
9 years ago
Not understanding, Can anyone explain it to me?
RAJESH KUMAR said:
9 years ago
Yes, you are right! @Amandeep I also have this confusion.
Amandeep said:
9 years ago
I'm not getting this. Once we press enter the if statement is not satisfied hence it should execute the printf statement and print the last char entered and that's it. There is no further call to the function. How can it print it in reverse order? please somebody explain me.
Manisha said:
9 years ago
In order to understand it clearly, we can break this program as below (considering input as abc)
void fun()
{
char c;
if((c = getchar())!= '\n')
{
char c;
if ((c = getchar())!= '\n')
{
char c;
if ((c = getchar())!= '\n')
{
..
}
else
{
printf("%c", c);
}
}
else
{
printf("%c", c);
}
}
else
{
printf("%c", c);
}
}
void fun()
{
char c;
if((c = getchar())!= '\n')
{
char c;
if ((c = getchar())!= '\n')
{
char c;
if ((c = getchar())!= '\n')
{
..
}
else
{
printf("%c", c);
}
}
else
{
printf("%c", c);
}
}
else
{
printf("%c", c);
}
}
Deepak Chauhan said:
10 years ago
@Vasuveer singh what you wrote is theoretically correct but here after asking for a character again it is asking for character.
Hence after 'a' is typed and enter key is pressed it will take enter ='\0' as input for next character and will exit the loop.
Hence after 'a' is typed and enter key is pressed it will take enter ='\0' as input for next character and will exit the loop.
Pravas said:
10 years ago
Answer is cba because, in the loop if((c=getchar()!="\n").
That means output character should not c ="\n" that is a (c!=a), so, first c is printed then b then a.
That means output character should not c ="\n" that is a (c!=a), so, first c is printed then b then a.
Manali said:
2 decades ago
Can you explain it to me. It took abc alltogether as an input, then printf will print it in reverse order? I am unable to understand it.
Vasuveer singh said:
1 decade ago
Hello friend,
At the very beginning the input is "a" so condition holds true hence it creates the first recursive call.
Now the input is "b" so condition holds true hence it creates the second recursive call.
Now the input is "c" so condition holds true hence it creates the third recursive call.
Now the input is "enter" so condition holds false hence it do not creates the recursive call but goes to print command, which prints "c".
Now control goes back to 2nd call of fun() in which c = 'b'.
Now control goes back to 1st call of fun() in which c = 'a'.
At the very beginning the input is "a" so condition holds true hence it creates the first recursive call.
Now the input is "b" so condition holds true hence it creates the second recursive call.
Now the input is "c" so condition holds true hence it creates the third recursive call.
Now the input is "enter" so condition holds false hence it do not creates the recursive call but goes to print command, which prints "c".
Now control goes back to 2nd call of fun() in which c = 'b'.
Now control goes back to 1st call of fun() in which c = 'a'.
Durai said:
1 decade ago
How come 'char c' prints three characters continuously as array?
Bhargav said:
1 decade ago
Why void fun() is declared inside the main ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers