# GATE2006-IT-50

9.9k views

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$

edited
2
swap( ) only change the addresses , not the values
4
i get it, that the answer is d, as i calculated value of a as 6 and b as 3.

okay. but how does the loop run agai? i is 5 when the printab function is called, so the loop should not execute even once
4

Continue keyword is used to skip the statements in the loop and directly go to the next iteration right ?

 while (i <= 4)
{
if ((i++)%2 == 1) continue;
a = a + i;
b = b + i;
}

so for odd values of i it skips which means a and b is added with 2 and 4

why is odd value considered ?

1
it is not skipping odd values it is just taking odd values because odd%2=1 , continue means if it is not then again do it so even values of i will not be added in a or b
0
while (i <= 4)
{
if ((i++)%2 == 1) continue;
a = a + i;
b = b + i;
}

It means if odd no. then Skip it because of continue keyword. Then why in  answer 1+3+5 is added.

1
Because the corresponding numbers are added after their increment. (0, 2, 4) are added to a and b after their increment. Therefore, (1+3+5) are added.
0
Yes.
0
1

My one and only doubt is:

"WHY IS STATIC VARIABLE REINITIALIZED TO '0' DURING 2nd FUNCTION CALL"
Since value of static variable is initialized only ones!

Please see this for reference :

1
static line execute only once, after the second function call of printab(), the first line will not be executed so a and b remain 6 and 3, but " i " initialize again by 0. So i is no more 5.
0

@RavGopal, I got it. Thank you 🙂

0
Think of if condition as

If(i%2==1)

{

i++;

continue;

}

Else

{

i++;

a=a+i;

b=b+i;

}

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
2
sir, why after the 2nd call to printab(), i becomes to 0?. As i is static variable so it must retains its value in 2nd call that was at the end of 1st call i.e 5.
46
because of i = 0; This is not an initialization but an assignment.
2
:-p, thanx
1
Can someone please explain the if condition part?

i is incremented on each condition. Initially i=0, In the first iteration, i++ will be incremented before checking the modulus. So, following that shouldn't even numbers be added to a and b?

14
i++ returns the value of "i" and so the value of i is used for mod operation and after this only i is getting incremented- it is post increment. "post" means after returning the current value of "i" for the expression, the value of i is incremented sometime before the next sequence point.
0
Thank you @Arjun Sir.

Missed the sequence points. So there's no effect on this expression by parenthesis.
5
parenthesis is useless here because ++ anyway has higher precedence than %. Parenthesis just clears this for the reader.

i++ happens first, but this operation always returns the value of i, and i gets incremented later as a side-effect.
2
Sir how can 5 be added???? Because 4%2!=0.. So only 1 and 3 must be added. Correct me if i am wrong
6
As here post increment is used ...when i=0 the if part evaluates to false and after that value of i becomes 1 i.e. before incrememting a and b,and so 1 is added to a and b.Next time i=1 is compared in if which  evaluates to true so continue gets executed (Note here value of i becomes 2).....Similarly i=2 is compared and condition becomes false and 3 gets added to a and b.Similarly  5 is added to a and b.
0
After 1st function call, output is 6 3. On second call, i again changes to 0 but a & b remains same i.e. a=6, b=3.

After 2nd call finishes, a=a+ 9= 6+9 = 15 and b= b+9 = 3+9 = 12.
1
the while loop runs until i is less than or equal to 4 why operating the loop till i = 5??
0
here only i is static or i,a,b all are static??
2
All 3 variables are static.
0
Why i changes to 0, if it is static variable?
5
Because making i static and assigning it '0' are in different line of code.
0
That doesn't make any difference I think.

We cannot reinitialize static variable again,it can only be initialized at once during start.
0
@Arjun sir, sequence point is the if statement no?
0
yes.
0
@arjun sir difference between initialization and assignment in c ??? anyone??
4
@Puja Initialization is the assignment done when memory is created.
0
it means we will do modulus first and then according to post increment value of i will be incremente later
1
yes in post increment, expression will be evaluated first then increment will happen,

(i++)%2 => i%2; I++;
0
Why swap function doesn't swap the values, it is call by reference right?
0
Sir may you please tell why swap() has no effect on values of a & b? As it's call by reference.
0
So you are saying that C language is having call by reference?
2
Sir now I understood from one of your answer, related to call by value and reference in C. C has only pass by value.
0
Can someone explain what's happening inside the swap function
0

swap will not work here becoz here swapping the addresses not value right?

1

@jk_1, value stored in pointer which is address of variables will change.

0
if ((i++)%2 == 1) continue;

what does this line mean

i++; if( i % 2 == 1)   or    if ( i % 2 == 1)  i++;

If I think as post increment then 2nd one is correct,but confusion arises because of ( ) given

0
Sir, had it been static int i=0, what should be output for second call of printab().
2
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;

*x=*y;

*y=*Temp;

Then definitely value will be swapped
0
Right.
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
0

@ prashant in the above link it says  " It is also possible to use ++i or --i. The difference is is that with ++i (prefix incrementing) the one is added before the “for loop” tests if i < 10. With i++ (postfix incrementing) the one is added after the test i < 10."

is it right ....??? plz clear my doubt.

2
Why i is reintianlize to 0?

, as i is static variable and intialize only once, in second printable() i should star with 5,

Correct me if wrong somewhere!
1
when i=5 then loop terminates then  how i=5 is calculated?
1
In for loop prefix increment and postfix increment both are same
But in while loop make difference
i++ means first test condition then increment
++i means first increment then test condition

0

@Ram Swaroop

since the brackets have more precedence then why (i++) is not incrementing first and then checking the condition ?

In your example given you have used ++i and not (++i).

0

same doubt ...did u got it?

0
If we use bracket then definitely  bracket have higher precedence than any increment or decrement operator

But in example we are using only i++ and ++Iso it work normal as post increment and pre increment operator work.
0

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

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

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

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

0

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

otherwise it has not affect expression.

$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
0

THANKS.
        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$

a =  6, b = 3
a =  15, b = 12

i.e option D
0
@arjun In the 2nd printab() call, i value will be 5 since it is static variable and we cannot re-initialize it. So why a=15,b=12?

the given code can be simplified as the code below without changing its meaning and output.

#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)
{
//we have to increment i irrespective of the number being odd or even
if (i % 2 == 1) //if i is odd
{

i++; // i is incremented here when it is odd
continue;
}

i++; //i is incremented here when it is even
a = a + i;
b = b + i;
}

//we can remove the swap function since it only swaps the pointers to a,b and has no effect on a,b values themselves.
//swap(&a, &b);
printf("a =  %d, b = %d\n", a, b);
}
int main()
{
printab();
printab();
return 0;
}

## Related questions

1
6k views
Which one of the choices given below would be printed when the following program is executed? #include <stdio.h> int a1[] = {6, 7, 8, 18, 34, 67}; int a2[] = {23, 56, 28, 29}; int a3[] = {-12, 27, -31}; int *x[] = {a1, a2, a3}; void print(int *a[]) { printf("%d,", a[0][2]); printf("%d,", ... } main() { print(x); } $8, -12, 7, 23, 8$ $8, 8, 7, 23, 7$ $-12, -12, 27, -31, 23$ $-12, -12, 27, -31, 56$
Which one of the choices given below would be printed when the following program is executed ? #include <stdio.h> struct test { int i; char *c; }st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"}; main () { struct test *p = st; p += 1; ++p -> c; ... ); } $\text{jungle, n, 8, nclastor}$ $\text{etter, u, 6, ungle}$ $\text{cetter, k, 6, jungle}$ $\text{etter, u, 8, ncestor}$
Consider the C program below. What does it print? # include <stdio.h> # define swapl (a, b) tmp = a; a = b; b = tmp void swap2 ( int a, int b) { int tmp; tmp = a; a = b; b = tmp; } void swap3 (int*a, int*b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main () { int num1 ... (num1 + 1, num2);} if (num1 > = num2) {swap3 (&num1, &num2);} printf ("%d, %d", num1, num2); } $5, 5$ $5, 4$ $4, 5$ $4, 4$