C Programming Reference >> C Programming Reference Question Bank >>
19/07/07 - Views - Ratings : 0 of 5 / Votes
Level - Advance
Question -
Output of the following code will be?
#include <stdio.h>
void e(int );
int main( )
{
int a;
a=3;
e(a);
return 0;
}
void e(int n)
{
if(n>0)
{
e(--n);
printf("%d" , n);
e(--n);
}
}
|
Answer -
0120
Explanation -
This is the most difficult breed of output questions, recursion outputs. To find an ouput in recursion questions of c you need to trace every step in detail as following figure does in this question.
This figure adequately explain the reason of output of c source code in this question.
Reader Comments -
| anonymous
|
A simple way to deal with small inputs (3 in this case) is to trace in reverse.
For e(negative number), output would be nothing
For e(0), output would be nothing
For e(1), the if statements would be e(0); printf(..., 0); e(-1) output would be 0
For e(2), the if statement would be e(1); printf(..., 1); e(0) output would be 01
For e(3), the if statement would be e(2); printf(..., 2); e(1) output would be 0120
|
|