Knowee
Questions
Features
Study Tools

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

🧐 Not the exact question you are looking for?Go ask a question

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:

  1. The function count() is called with n=3 from the main() function.
  2. The static variable d is initialized to 1.
  3. The printf() function prints n and d, which are 3 and 1 respectively.
  4. The value of d is incremented to 2.
  5. The function count() is called again with n=2.
  6. This time, the static variable d is not reinitialized, it retains its previous value which is 2.
  7. The printf() function prints n and d, which are 2 and 2 respectively.
  8. The value of d is incremented to 3.
  9. The function count() is called again with n=1.
  10. The printf() function prints n and d, which are 1 and 3 respectively.
  11. The value of d is incremented to 4.
  12. Since n is not greater than 1, the function count() is not called again.
  13. The printf() function prints d, which is 4.
  14. The control goes back to the previous call of count() and prints d, which is 4.
  15. 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".

This problem has been solved

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

1/3

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.