What will be the output of this program ?
please give the complete detailed solution of this program ..
#include<stdio.h> void fun(int); typedef int (*pf) (int, int); int proc(pf, int, int); int main() { int a=3; fun(a); return 0; } void fun(int n) { if(n > 0) { fun(--n); printf("%d,", n); fun(--n); } }
Here's a detailed analysis of your program. Before proceeding, there seem to be syntax errors in your code:
1. You didn't include a library. There's a #include
directive but it doesn't specify any library.
2. The typedef
line is not complete and it's not used anywhere in the program.
After addressing the errors, the remaining part of the program is:
#include <stdio.h>
void fun(int);
int main() {
int a=3;
fun(a);
return 0;
}
void fun(int n) {
if(n > 0) {
fun(--n);
printf("%d,", n);
fun(--n);
}
}
This program does the following:
1. It includes the standard input/output library (stdio.h).
2. It declares a function fun(int)
that doesn't return a value (`void`). This function is defined later in the program.
3. int main()
is the entry point for the program.
4. Inside main()
, an integer a
is declared and initialized with the value 3.
5. The function fun(a)
is called with a
as its argument.
6. The program then returns 0
as a standard way to indicate that it finished successfully.
7. The function fun(int n)
is defined. It takes an integer n
as input.
8. Inside fun(int n)
, there's a conditional check. If n
is greater than zero, the program:
- Calls fun(--n)
. --n
is a pre-decrement operator which decreases n
by 1 before the function call.
- Then, it prints the value of n
to the console.
- Calls fun(--n)
again, further decreasing n
by 1 before the function call.
The function fun(int n)
is a recursive function. It calls itself until n
becomes zero, which is the base case to stop the recursion.
The result of this program will be the output of all the printf statements in the recursion tree of the function fun
.
When you run the program, the output will be 0,1,0,
.
Here's how this output is generated:
1. The first fun(--n)
call reduces n
from 3 to 2. This 2 is then passed to fun()
.
2. The first fun(--n)
call in the second level reduces n
from 2 to 1. This 1 is then passed to fun()
.
3. The first fun(--n)
call in the third level reduces n
from 1 to 0. This 0 is passed to fun()
.
4. Because n
is not greater than 0, this fun()
call returns without doing anything.
5. We're back to the third level, where n
is printed (`0`), and then the second fun(--n)
is called. Because n
is already 0, it can't be reduced any further, so fun()
is called with n
as 0.
6. This fun()
call also returns without doing anything because n
is not greater than 0.
7. We're back to the second level, where n
is printed (`1`), and then the second fun(--n)
is called, with n
reduced from 1 to 0. The process from steps 4-6 is repeated.
8. We're back to the first level, where n
is printed (`0`), and then the second fun(--n)
is called, with n
reduced from 1 to 0. The process from steps 4-6 is repeated.
In summary, the recursive function fun(int n)
prints a series of decreasing numbers, skipping every second number, down to 0
. After printing 0
, it stops due to the if (n > 0)
condition. The sequence is created by reducing n
by 1 before each fun()
call and printing n
between the first and second fun()
calls.