in Programming in C edited by
14,908 views
64 votes
64 votes

Consider the C program given below. What does it print?

#include <stdio.h>
int main ()
{
        int i, j;
        int a [8] = {1, 2, 3, 4, 5, 6, 7, 8};
        for(i = 0; i < 3; i++) {
             a[i] = a[i] + 1;
             i++;
        }
        i--;
        for (j = 7; j > 4; j--) {
              int i = j/2;
              a[i] = a[i] - 1;
        }
        printf ("%d, %d", i, a[i]);
}
  1. $2, 3$
  2. $2, 4$
  3. $3, 2$
  4. $3, 3$
in Programming in C edited by
14.9k views

3 Comments

edited by
see that variable named i is created in two places.one is in main() function and other in for loop block.This way you will find that you are wrong
16
16
edited by

Things to note:

  • In the first for-loop, there are two increments to i.
  • In the second for-loop, a local i is declared.
  • In the print statement, the i of the main function would be used, because that's the one within its scope
9
9
Easy
0
0

3 Answers

146 votes
146 votes
Best answer

Answer is (C)  $3,2$

First $2$ variable integer type declared named $i$ and $j$ 

Then int type array $a[8]$ declared and initialized.

$a[0] = 1 , a[1] = 2, a[2] = 3, a[3] = 4, a[4] = 5, a[5] = 6, a[6] = 7,a[7] = 8$

Then for loop started

$i=0 , i<3$ (true)

      $a[0]=a[0]+1 = 1+1=2$      

        $i++$ (outside for loop) ,  $i++$ (inside for loop);

$i=2 ,i<3$ (true)

      $a[2]=a[2]+1 = 3+1=4   $

       $ i++, i++$(outside for loop) ,

$i=4,  i<3$ (false)   //Now come out of loop

    $i-- ;$  (so $i=3$)

Now another for loop started where in loop integer type variable named i declared 

Block Scope: A Block is a set of statements enclosed within left and right braces ({ and } respectively). Blocks may be nested in C (a block may contain other blocks inside it). A variable declared in a block is accessible in the block and all inner blocks of that block, but not accessible outside the block.

What if the inner block itself has one variable with the same name?
If an inner block declares a variable with the same name as the variable declared by the outer block, then the visibility of the outer block variable ends at the point of declaration by inner block.

So here inner block  int i has the scope in this block only and outer block int i visibility is not allowed in that block

$j=7 ,  j>4$(true)

   int $i = 7/2=3$

   $a[3]=a[3]-1=4-1=3$

$j=6 ,   j>4$ (true)

   int $i = 6/2=3$

   $a[3]=a[3]-1=3-1=2$

$j=5 ,  j>4$ (true)

   int $i = 5/2=2$

   $a[2]=a[2]-1=4-1=3$

$j=4 ,   j>4$ (false)

Now when the for loop ends its variable named $i$ scope is also end and the outer block variable now visible. So, in printf outer variable $i$ is used.

So, the output would be: $3,2$.

edited by

4 Comments

What would be the output if in the for loop we have for(int i=0;i<3;i++)and the rest is same as in the given question?
0
0
Can anyboday explain how that 4-1 will come
0
0
Because in the previous for loop the value of a[2] was updated to 4 and the array is visible inside both the loops.
0
0
62 votes
62 votes

I haven't explain the solution of question here, as it is already well explained in selected answer. I explained here a concept that was not obvious for me.
(Technically it should be put as comment but i am making it answer because not everyone reads all comments.)

We know that if we declare any variable more than once then there will be error of redeclaration like this

int i;
int i;


Error: redeclaration of 'i' with no linkage.

In question, "for" loop is doing the same thing, just redeclaring for every iteration, isn't it ?
There must be an error but there won't be !
At first, when I saw the code in question I thought there is an error because we are declaring a variable inside. But is not an error.

See two interesting codes below-
 

Code -1

for(j=0; j<10; j++)
{
int i;
}
Code-2

for(j=0; j<10; j++)
int i;

Aren't they similar ?
These both codes looks similar, but Code-2 will produce error and code-1 won't.

You know why ?

its all about "block". Anything enclosed with bractes {} called a block.
"{ }" this bracket destroy lifetime of any variable therefore if i use brackets then at each iteration new variable is created and destroyed once it sees "}" i.e. for next iteration.
Using bracket creation/destroy happens for  each iteration but without bracket existing old variable is not destroy, therefore it throws error of redeclaration. 

This is another good read : http://stackoverflow.com/questions/22527846/scope-and-lifetime-of-local-variables-in-c

edited by

4 Comments

Sir, code2:

for(j=0; j<10; j++)
int i;

 2nd code not throwing an error of "redeclaration", it is throwing an error of "expected error before int".And this error mainly occurs because when after conditional statement if you put any declaration statement.
0
0

@  In 1st for loop ' i ' refers to the variable ' i ' which has visibility throughout the main() & i-- is obviously refers to that ' i '. that's why i-- is taking that value.actually in 1st for loop inner block concept doesn't work.

In next for loop ' i ' refers to the variable ' i '  which is declared only for that for loop & it's visibility is also upto the the block of every iteration of that for loop.

1
1
Wow,सर

Next level
0
0
3 votes
3 votes
Be careful about the scope of i,
there are two variables named: i, with different scope.

There are 2 main points to consider while solving this question. Scope of variable i and integer division.
First for loop will run for i = 0, 2 and 4 as i is incremented twice inside loop and resultant array will be a  = 2, 2, 4, 4, 5, 6, 7, 8  (Loop will terminate at i = 4)
After that i value is 3 as there is a decrement operation after for loop.
Next for loop is running for j = 7, 6 and 5 and corresponding i values which is a local variable inside for loop will be 3 (7/2), 3 (6/2) and 2 (5/2). Array after this for loop will be
a  = 2, 2, 3, 2, 5, 6, 7, 8
After the for loop, current i value is 3 and element at a[3] = 2.
Answer:

Related questions