IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C# Programming Java Programming
Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

C Programming - Strings - Discussion

@ : Home > C Programming > Strings > Find Output of Program - Discussion

Read more:

"I never think of the future. It comes soon enough."
- Albert Einstein
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);
}

[A]. abc abc[B]. bca
[C]. Infinite loop[D]. cba

Answer: Option D

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


Manali said: (Sun, Jun 27, 2010 11:49:51 AM)    
 
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.

Sundar said: (Tue, Feb 22, 2011 07:02:05 AM)    
 
@All

In Turbo C, I have compiled the program myprogram.c and executed as given below.

C:\Turboc>myprogram.exe
kumar
ramuk

C:\Turboc>myprogram.exe
sundar kumar
ramuk radnus

C:\Turboc> _


Note: Give the input by simply typing any string and press .

The given program prints the given string in reversed order.

I hope this example will help you. Have a nice day!

Sagar Kapadiya(Surat) said: (Thu, Jul 14, 2011 07:38:46 AM)    
 
Can you describe a whole program?

Reshma said: (Thu, Aug 25, 2011 10:19:59 PM)    
 
Can anybody tell me how abc is reversed to cba ?

Kamal Dua said: (Mon, Oct 3, 2011 05:40:14 PM)    
 
It is based on flow of control.

Firstly in function fun() 'a' is entered then condition is checked. as it is not equal to '\0' so again fun() is called and this time 'b' is entered. similarly 'c' and at last in 4th recursive call of fuction fun()when enter is pressed, condition satisfies. so cursor goes to next line. now contol goes back to 3rd call of fun() in which c='c'.

Similarly control goes back to 2nd and 1st fun() call in which 'b' and 'a' get printed.

Rohan said: (Tue, Feb 7, 2012 11:48:19 AM)    
 
Thanks kamal.

Write your comments here:
Name *:     Email:


© 2008-2011 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Advertise     Contact us: info@indiabix.com     Follow us on twitter!