edited by
1,500 views
1 votes
1 votes

After $3$ calls of the $c$ function bug ( ) below, the values of $i$ and $j$ will be :

int j = 1;
bug ()
{
    static int i = 0; int j = 0;
    i++; j++;
    return (i);
}
  1. $i = 0, j = 0$
  2. $i = 3, j = 3$
  3. $i = 3, j = 0$
  4. $i = 3, j = 1$
edited by

2 Answers

0 votes
0 votes

D would be the correct answer.

Because static variable so it will retain its value during each function call so the value of after 3 function calls would be 3 whereas j(inside the bug() function) is non-static variable so j would be initialized with every function call so after last call value of j=1.

0 votes
0 votes
Answer: D

For “i” value, I am satisfied with @Divyanshu Shukla explanation.

For “j” value, I think its value after the 3 function calls is independent of “bug()” function. Because after the function call memory allocated to “bug() function ” will be removed from the stack frame. So j = 1, is because of globally defined value of j( First line “int j = 1;”).

Related questions

1 votes
1 votes
8 answers
1
go_editor asked Mar 26, 2020
2,317 views
Find the output of the following $“C”$ code :Main() { int x=20, y=35; x=y++ + x++; y=++y + ++x; printf (“%d, %d\n”, x, y); }$55, 93$$53, 97$$56, 95$$57, 94$
3 votes
3 votes
2 answers
2