Knowee
Questions
Features
Study Tools

Select the correct answerPredict the output of below given code in C.#include <stdio.h>void Function(){  static int i=10;  if(--i){    main();    printf("%d ",i);  }}int main(){     Function();  return 0;}Options9 8 7 6 5 4 3 2 10 0 0 0 0 0 0 0 0 Compile time errorCode execution is clean but nothing will be printed

Question

Select the correct answerPredict the output of below given code in C.#include <stdio.h>void Function(){  static int i=10;  if(--i){    main();    printf("%d ",i);  }}int main(){     Function();  return 0;}Options9 8 7 6 5 4 3 2 10 0 0 0 0 0 0 0 0 Compile time errorCode execution is clean but nothing will be printed

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

Solution

The correct answer is "9 8 7 6 5 4 3 2 1".

Here's the step by step explanation:

  1. The main function calls the Function() which has a static variable 'i' initialized to 10.

  2. Static variables retain their values between function calls, so 'i' will not be reinitialized to 10 every time Function() is called.

  3. The if condition checks if --i is true. Since 'i' is 10 at first, --i will decrement it to 9 and the condition is true (non-zero), so it enters the if block.

  4. Inside the if block, main() is called again, which calls Function() again. Now 'i' is 9, so --i will decrement it to 8 and the condition is still true. This process repeats until 'i' is decremented to 0, at which point the condition is false and it doesn't enter the if block.

  5. After each call to main(), printf("%d ",i) is executed, but since this is after the recursive call to main(), these print statements will only be executed on the way back up the recursion, after 'i' has been decremented to 0 and the if condition is false.

  6. So the output will be the values of 'i' from 9 down to 1, printed in that order.

This problem has been solved

Similar Questions

Select the correct answerPredict the output produced by below given code snippet in C.#include<stdio.h>void Function(char *S1, char *S2){ char *Buffer = S1; S1 = S2; S2 = Buffer;}    int main(){ char *S1 = "Code Tantra"; char *S2 = "Coding Castle"; Function(S1, S2); printf("%s,%s", S1, S2); return 0;}Optionschar *S1 = "Code Tantra"; char *S2 = "Coding Castle"; these statements throw segmentation faultThis code prints "Code Tantra,Coding Castle"This code prints "Code Tantra,Code Tantra"This code prints "Coding Castle,Code Tantra"Code throws compile time error

What will be the output of the following C code?

Select the correct answerWhich operator is used for the bitwise AND assignment in C?Options&=&&&&&=

Select the correct answerPredict the output of below given code.#include <stdio.h>int Function(char *S1){ char *S2 = S1; while(*++S1); return (S1-S2);} int main(){ char *Str = "CodeTantra Coding Castle"; printf("%d", Function(Str)); return 0;}Options2524Segmentation Fault26Garbage Value

What is the output of the following program?c = 4231for t in c:  print(t)Options42314 3 2Error4231

1/4

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.