in Programming in C edited by
25,569 views
84 votes
84 votes

Which one of the choices given below would be printed when the following program is executed?

#include <stdio.h>
void swap (int *x, int *y)
{
    static int *temp;
    temp = x;
    x = y;
    y = temp;
}
void printab ()
{
    static int i, a = -3, b = -6;
    i = 0;
    while (i <= 4)
    {
        if ((i++)%2 == 1) continue;
        a = a + i;
        b = b + i;
    }
    swap (&a, &b);
    printf("a =  %d, b = %d\n", a, b);
}
main()
{
    printab();
    printab();
}
  1. $a = 0, b = 3$
    $a = 0, b = 3$
  2. $a = 3, b = 0$
    $a = 12, b = 9$
  3. $a = 3, b = 6$
    $a = 3, b = 6$
  4. $a = 6, b = 3$
    $a = 15, b = 12$
in Programming in C edited by
25.6k views

4 Comments

It is evident from code that i, a, b are static variables. So how are we again executing i=0 during second call of printab().. Please help.
1
1
D
0
0
If i variable is static , it should retain the value when the function is called second time, then 12 and 15 will not be printed.  Please clarify
0
0

6 Answers

86 votes
86 votes
Best answer

First of all, the swap function just swaps the pointers inside the function and has no effect on the variables being passed. 

Inside printab, a and b are added odd integers from $1$-$5$, i.e., $1+3+5 = 9$. So, in first call to printab, $a = -3 + 9 = 6$ and $b = -6 + 9 = 3$. 

Static variables have one memory throughout program run (initialized during program start) and they keep their values across function calls. So, during second call to printab, $a = 6 + 9 = 15$, $b = 3 + 9 = 12$.

Hence, (D) is choice. 

edited by
by

4 Comments

@Harshq 

In swap function x and y have address of a and b. And they are swapping address.
If there is code segment like

*Temp=*x;    // dereferencing the uninitialized pointer

*x=*y;

*y=*Temp;

Then definitely value will be swapped

This is wrong because temp is static integer pointer in the question and you are dereferencing the uninitialized pointer (runtime error). 

To make it work right take temp as integer and use temp only and not *temp.

1
1

@ankitgupta.1729 @Abhrajyoti00

How does the static variable behave if used in two different function.

e.g.

printab()

static int a=-3;

}

main()

{

static int a = 5;

printab();

}

0
0
edited by

@GateOverflow04 

Static has scope within the body (where it is initialized) but it’s lifetime is throughout the program. Thus for the main function variable $a$ will be initialized in data segment with value $5$ which is local only to the main function.

And another variable $a$ will be initialized in data segment with value $-3$ which is local only to the printab() function.

Since it is in data segment it's value can be altered any time, but can't be initialized more than once. Also the value of 'a' in main() and the value of 'a' in the printab() function are independent of each other  and can't be used anywhere outside their own scope.

This will be useful for static variables : https://gateoverflow.in/2176/gate-cse-2012-question-48?show=386635#c386635

1
1
29 votes
29 votes
while (i <= 4)
{
    if ((i++)%2 == 1) continue; // key point of program here if comdition true then go to while loop directly 
    a = a + i; // will process when i= even 
    b = b + i; // will process when i= even 
 }

Inside printab, a and b are added odd integers from

for i= 1 a= -3 + 1 = -2  and b = -6+1= -5 

for i= 3 a= -2 + 3 = 1  and b = -5+3= --2 

for i= 5 a= 1 + 5 = 6  and b = -2 +5= 3

here printf(a,b) = 6,3 

if static int i= 0; given in code then only old value of i is used. 
since static int i and i= 0 is given so for next printab () i= 0 is taken.
 By looking 1st  printf(a,b) = 6,3 

Ans is D

reffer for continue:https://www.codingunit.com/c-tutorial-for-loop-while-loop-break-and-continue

edited by

4 Comments

@
int i=0;
" if ((i++)%2 == 1) "

why i is incremented after %2 operation is done even though i++ is in bracket 

 

 

0
0
You are talking about the actual gate questionsorry I think wrong  you are asking about my comment

@googlegoku
We know unary operators (++,--) have higher precedence than arithmetic operators but here () have useless because of post increment operator
you can try this code
int main(void) {

int i =0;
while (i<=4)
{
if ((i++)%2==1) continue;

printf ("%d", i);
}
   
    return 0;
}

Answer 135
0
0
edited by

i think () operator has higher presendence than % in case of  use it as function call

otherwise it has not affect expression.

0
0
25 votes
25 votes

$main()$

$\downarrow$

$printab()$

$\fbox{static i=0}$ $\fbox{static a=-3}$$\fbox{static b=-6}$

$i=0$  

$1.while(0<=4)\{$

$if(0\%2==1)// condition\ false$

$\fbox{i=1}$

$a=-3+1=-2$

$b=-6+1=-5$

$2.while(1<=4)\{$

$if(1\%2==1)\ continue;$

$\fbox{i=2}$

$3.while(2<=4)\{$

$if(2\%2==1)// condition\ false$

$\fbox{i=3}$

$a=-2+3=1$

$b=-5+3=-2$

$4.while(3<=4)\{$

$if(3\%2==1)\ continue;$

$\fbox{i=4}$

$5.while(4<=4)\{$

$if(4\%2==1)// condition\ false$

$\fbox{i=5}$

$a=1+5=6$

$b=-2+5=3$

$6.while(5<=4)// condition\ false$

$\downarrow$

$swap(\&a,\&b)$

Important to notice: It will swap the pointers not the values.

Print the values of $a,b=6,3$


Again call $printab();$

.

.

Print the values of $a,b=15,12$

$Ans: D$

edited by

4 Comments

You are perfect sir,using visuals is always helpful

 

Thanks
0
0
Point to point explantion
0
0
Thanks a lot sir
0
0
7 votes
7 votes
        if ((i++)%2 == 1) continue;

This is the heart of the code. continue will make the compiler skip rest of the lines in the loop, and go straight back to checking the loop condition again.
Post increment will increment i after checking the condition.

When i = 0, we won't hit continue. So, add 1 (and not 0, because post-increment)

When i = 1, we hit continue. So skip rest of the lines.

When i = 2, add 3.

When i = 3, we hit continue.

When i = 4, add 5.

Break out of while loop now. Because i = 5.

 

So finally; $a = -3 + 1 + 3 + 5 = 6$. And $b = -6 + 1 + 3 + 5 = 3$

Swap doesn't do anything to the values inside a and b, so a and b stay intact.

a = 6, b = 3

Option D.

 

Now, when printab() is called the second time, i = 5. But in the immediate next line, i = 0. So, while loop will run the same.
$a = 6 + 1 + 3 + 5 = 15$
$b = 3 + 1 + 3 + 5 = 12$

Answer:

Related questions