edited by
13,712 views
45 votes
45 votes

Consider the following C code segment.

int a, b, c = 0; 
void prtFun(void); 
main()
{ 
    static int a = 1;       /* Line 1 */
    prtFun(); 
    a += 1;
    prtFun();
    printf(“ \n %d %d ”, a, b);
}

void prtFun(void)
{
    static int a = 2;       /* Line 2 */
    int b = 1;
    a += ++b;
    printf(“ \n %d %d ”, a, b);
}

What output will be generated by the given code segment?
 

  1. $\begin{array}{llll} 3 &  & 1 &  \\  4 &  & 1 &  \\  4 &  & 2 &    \end{array}$
  2. $\begin{array}{lll} 4 &  & 2 &  \\  6 &  & 1 &  \\  6 &  & 1 &   \end{array}$ 
  3. $\begin{array}{lll} 4 &  & 2 &  \\  6 &  & 2 &  \\  2 &  & 0 &    \end{array}$ 
  4. $\begin{array}{lll} 3 &  & 1 &  \\  5 &  & 2 &  \\  5 &  & 2 &   \end{array}$
edited by

4 Answers

Best answer
69 votes
69 votes

main
$a=1$
$prtFun()$
$a=2$
$b=1 $
$a= a  + \text{++}b = 2+2 = 4$
$b = 2$
printf  $\rightarrow 4  \ 2$
back to main
$a = a+1 \rightarrow 1+1 \rightarrow 2$ (local static a is taken)
$prtFun()$
$a=4$ // previous  value in the function is retained because of static
$b=1$
$a= a  + \text{++}b = 4+2 = 6$
$b = 2$
printf $\rightarrow 6 \ 2$
back to main
$a = 2$
$b = 0$ (initial value of global $b$. in $prtFun$ local b is only updated)
printf $\rightarrow 2 \ 0$
 

The answer is C.

2 votes
2 votes
>  first, execute prtFun() as it is called under main() twice. ‘a’ and ‘b’ are global variables as declared before main(). When prtFun() is called the first time, the local ‘b’ becomes 2- as increment of b is done before due to ++b and local ‘a’ becomes 4 (a= a+b= 2+2).

> When prtFun() is called the second time, the same instance of local static ‘a’ is used and a new instance of ‘b’ is created or initialized again because ‘a’ is static and ‘b’ is non-static. So ‘b’ becomes 2 again and ‘a’ becomes 6 (a= 4+2=6).

>main() also has its own local static variable named ‘a’ that hides the global ‘a’ in main (because when the global and local variable has the same name then local variable gets the precedence). The printf() statement in main() accesses the local ‘a’ and prints its value as 2. The same printf() statement accesses the global ‘b’ as there is no local variable named ‘b’ in main. Also, the default value of static and global int variables is 0. That is why printf statement in main() prints 0 as the value of b.
1 votes
1 votes

STATIC:It will be executed only once in it’s lifetime.

Solution:

Let’s give the numbering for the better understanding-

 

int a, b, c = 0;  
void prtFun(void); 
main()
{                                                    
    1-→ static int a = 1;       /* Line 1 */    
    2-→prtFun();                               
                                                 
    3-→a += 1;
    4-→prtFun();
    5-→printf(“ \n %d %d ”, a, b);
}

void prtFun(void)
{
    6-→static int a = 2;       /* Line 2 */
    7-→int b = 1;
    8-→a += ++b;
    9-→printf(“ \n %d %d ”, a, b);

 

 

1-->a=1(Now this will never be executed coz of static variable)

2—→ 6→ a=2(Same reason this will never be executed again due to static variable)

       7→ b=1

       8→ a+ ++b=2+2=4

              So a=4,b=2

       9→ O/P a=4,b=2

back to main

3 –→ a=a+1=1+1=2;

4--→ 6→now a=4 it won’t be changed to 2 because of the static variable.

          7→ b=1 Since b will bw reset to 1 again as it is not static

          8→ a=a+ ++b=4+2=6

          9→ a=6,b=2 o/p

back to main

5-→ a=2,b=0 o/p 

 

o/p  4 2

        6 2

        2 0

Answer:

Related questions

40 votes
40 votes
5 answers
2
48 votes
48 votes
4 answers
3
go_editor asked Apr 21, 2016
13,579 views
Consider the following relations $A, B$ and $C:$ $$\overset{\textbf{A}}{\begin{array}{|c|c|c|}\hline\\\textbf{Id}& \textbf{Name}& \textbf{Age} \\\hline12& \text{A...
42 votes
42 votes
3 answers
4