What will be the output of the following C program?void count(int n){ static int d=1; printf(“%d “, n); printf(“%d “, d); d++; if(n>1) count(n-1); printf(“%d “, d); } void main(){ count(3); }a.3 1 2 1 1 1 2 2 2b.3 1 2 1 1 1 2c.3 1 2 2 1 3 4 4 4d.3 1 2 2 1 3 4
Question
What will be the output of the following C program?void count(int n){ static int d=1; printf(“%d “, n); printf(“%d “, d); d++; if(n>1) count(n-1); printf(“%d “, d); } void main(){ count(3); }a.3 1 2 1 1 1 2 2 2b.3 1 2 1 1 1 2c.3 1 2 2 1 3 4 4 4d.3 1 2 2 1 3 4
Solution
The output of the given C program will be "3 1 2 2 1 3 4 4 4". So, the correct option is d.
Here's the step by step explanation:
- The function count() is called with n=3 from the main() function.
- The static variable d is initialized to 1.
- The printf() function prints n and d, which are 3 and 1 respectively.
- The value of d is incremented to 2.
- The function count() is called again with n=2.
- This time, the static variable d is not reinitialized, it retains its previous value which is 2.
- The printf() function prints n and d, which are 2 and 2 respectively.
- The value of d is incremented to 3.
- The function count() is called again with n=1.
- The printf() function prints n and d, which are 1 and 3 respectively.
- The value of d is incremented to 4.
- Since n is not greater than 1, the function count() is not called again.
- The printf() function prints d, which is 4.
- The control goes back to the previous call of count() and prints d, which is 4.
- The control goes back to the first call of count() and prints d, which is 4.
So, the output is "3 1 2 2 1 3 4 4 4".
Similar Questions
What will be the output of the following C code?#include<stdio.h>int main(){ int a=0, b=1, c=2; *((a+1 == 1) ? &b : &a) = a ? b : c; printf("%d, %d, %d\n", a, b, c); return 0;} Ops: A. 0,2,2 B. 2,2,2 C. 0,1,2 D. 1,1,2
What will be the output of the following C program?#include<stdio.h>int main(){ int a[5] = {5, 3, 15, 20, 25}; int i, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d, %d, %d", i, j, m); return 0;}
What is output of below program?int main(){int i,j,count;count=0;for(i=0; i<5; i++);{for(j=0;j<5;j++);{ count++;}}printf("%d",count);return 0;} (A) 55(B) 54(C) 0(D) 1
What will be the output of the following C code? #include void main() { int a[2][3] = {1, 2, 3, 4, 5}; int i = 0, j = 0; for (i = 0; i < 2; i++) for (j = 0; j < 3; j++) printf("%d", a[i][j]); }a.1 2 3 4 5 0b.1 2 3 4 5 junkc.1 2 3 4 5 5d.Run time error
What will be the output of the following C code?#include <stdio.h> void main() { m(); m(); } void m() { static int x = 5; x++; printf("%d", x); }6 76 65 55 6
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.