edited by
14,873 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$
edited by

3 Answers

Best answer
146 votes
146 votes

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

30 votes
30 votes
4 answers
2
Ishrat Jahan asked Oct 28, 2014
9,461 views
What is the output printed by the following C code?# include <stdio.h int main () { char a [6] = "world"; int i, j; for (i = 0, j = 5; i < j; a [i++] = a [j ]); printf ("...