Output of C Programs | Set 11
Last Updated :
27 Dec, 2016
Asked by Shobhit
C
#include<stdio.h>
int fun(int n, int *fg)
{
int t, f;
if(n <= 1)
{
*fg = 1;
return 1;
}
t = fun(n-1, fg);
f = t + *fg;
*fg = t;
return f;
}
int main( )
{
int x = 15;
printf ( "%d\n", fun (5, &x));
getchar();
return 0;
}
In the above program, there will be recursive calls till n is
not smaller than or equal to 1.
fun(5, &x)
\
\
fun(4, fg)
\
\
fun(3, fg)
\
\
fun(2, fg)
\
\
fun(1, fg)
fun(1, fg) does not further call fun() because n is 1 now, and it goes inside the if part. It changes value at address fg to 1, and returns 1.
Inside fun(2, fg)
t = fun(n-1, fg); --> t = 1
/* After fun(1, fg) is called, fun(2, fg) does following */
f = t + *fg; --> f = 1 + 1 (changed by fun(1, fg)) = 2
*fg = t; --> *fg = 1
return f (or return 2)
Inside fun(3, fg)
t = fun(2, fg); --> t = 2
/* After fun(2, fg) is called, fun(3, fg) does following */
f = t + *fg; --> f = 2 + 1 = 3
*fg = t; --> *fg = 2
return f (or return 3)
Inside fun(4, fg)
t = fun(3, fg); --> t = 3
/* After fun(3, fg) is called, fun(4, fg) does following */
f = t + *fg; --> f = 3 + 2 = 5
*fg = t; --> *fg = 3
return f (or return 5)
Inside fun(5, fg)
t = fun(4, fg); --> t = 5
/* After fun(4, fg) is called, fun(5, fg) does following */
f = t + *fg; --> f = 5 + 3 = 8
*fg = t; --> *fg = 5
return f (or return 8 )
Finally, value returned by fun(5, &x) is printed, so 8 is printed on the screen
Similar Reads
Output of C Programs | Set 1 Predict the output of below programs. Question 1c#include<stdio.h> int main() { int n; for(n = 7; n!=0; n--) printf("n = %d", n--); getchar(); return 0; }Output: Above program goes in infinite loop because n is never zero when loop condition (n != 0) is checked.Question 2c #include<stdio.h
2 min read
Output of C Programs | Set 16 Predict the output of below C programs. Question 1 C #include <stdio.h> char* fun() { return "awake"; } int main() { printf("%s",fun()+ printf("I see you")); getchar(); return 0; } Output: Some string starting with "I see you" Explanation: (Thanks to Venki for sug
2 min read
Output of C programs | Set 51 1. What will be the output of the below program? CPP #include <stdio.h> int main() { printf("%d", printf("%d", printf("%d", printf("%s", "Welcome to geeksforgeeks")))); return (0); } Options: 1. Welcome to geeksforgeeks2531 2. Welcome to geeksf
2 min read
Output of C Programs | Set 13 Difficulty Level: Rookie Question 1 Predict the output of below program. C int main() { char arr[] = "geeksforgeeks"; printf("%d", sizeof(arr)); getchar(); return 0; } Output: 14 The string âgeeksforgeeksâ has 13 characters, but the size is 14 because compiler includes a single '
2 min read
Output of C Programs | Set 14 Predict the output of below C programs. Question 1 C #include<stdio.h> int main() { int a; char *x; x = (char *) &a; a = 512; x[0] = 1; x[1] = 2; printf("%d\n",a); getchar(); return 0; } Answer: The output is dependent on endianness of a machine. Output is 513 in a little endian
2 min read
Output of C Programs | Set 15 Predict the output of below C programs. Question 1C #include<stdio.h> int main(void) { int a = 1; int b = 0; b = ++a + ++a; printf("%d %d",a,b); getchar(); return 0; } Output: 3 6Explanation: ++ has precedence over + and operates right to left. Hence rightmost ++a gives a=2 and the ++a left of
1 min read