edited by
17,485 views
66 66 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 if:

Line 1 is replaced by auto int $a = 1$;

Line 2 is replaced by register int $a = 2$; 

  1. $\begin{array}{ll}  \text{3}  & \text{1} \\ \text{4}  & \text{1} \\ \text{4}  & \text{2} \\ \end{array}$
  2. $\begin{array}{ll}  \text{4}  & \text{2} \\ \text{6}  & \text{1} \\ \text{6}  & \text{1} \\ \end{array}$
  3. $\begin{array}{ll}  \text{4}  & \text{2} \\ \text{6}  & \text{2} \\ \text{2}  & \text{0} \\ \end{array}$
  4. $\begin{array}{ll}  \text{4}  & \text{2} \\ \text{4}  & \text{2} \\ \text{2}  & \text{0} \\ \end{array}$

7 Answers

Best answer
51 51 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$


$prtFun()$
$a=2$ //previous a is lost
$b=1$
$a= a  + \text{++}b = 2+2 = 4$
$b = 2 $
printf $\rightarrow  4 \ 2$


back to main
$a = 2$
$b = 0$ (initial value of global b. in $prtFun$ local b is only updated)
printf $\rightarrow 2 \ 0$

Answer is D.

edited by
3 3 votes

I tried the same and I am getting C
Here is my code:

#include<stdio.h>

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);
        }
 

1 flag:
✌ Edit necessary (spongebob “Wrong Answer. Need to try with modified code”)
1 1 vote

Static is special but Register and Auto has nothing special in terms of Scope. So a simple execution follows.

0 0 votes

Shortcut to question. 

register keyword causes variabe to be stored in regster no effect extra. 
we removed static keyword from lin2

prtFun has no inputs, no static variables. So each time we call it executes as new. So both time calls it will give same output. And first 2 times prtFun's print stmt executd and at last main()'s print executed. 


So first two ines of output SHOULD be same 
Only option D qualifies this condition. 

0 0 votes

 Since we completely removed the static keyword from prtFun(), the function no longer remembers anything from its previous executions. Every single time you call prtFun(),  it starts completely fresh from scratch!

Answer:
Position:
Show:

Related questions

61 61 votes
6 answers 6 answers
20.7k
20.7k views
gatecse asked Sep 29, 2014
20,721 views
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); ...
63 63 votes
6 answers 6 answers
20.0k
20.0k views
gatecse asked Aug 5, 2014
20,005 views
What will be the output of the following C program segment?char inChar = 'A'; switch ( inChar ) { case 'A' : printf ("Choice A \ n"); case 'B' : case 'C' : printf ("Choic...
69 69 votes
5 answers 5 answers
23.0k
23.0k views
go_editor asked Apr 21, 2016
23,010 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...
53 53 votes
6 answers 6 answers
25.6k
25.6k views
go_editor asked Apr 21, 2016
25,648 views
For the grammar below, a partial $LL(1)$ parsing table is also presented along with the grammar. Entries that need to be filled are indicated as $E1, E2,$ and $E3$. $\var...