Tuesday, March 22, 2011

Program for further scope of variables with Explanation


Program

#include <stdio.h>
main()
{
    int k = 10;

    }
       for(int  i=0;i<2;i++)\\ A
       {      \\ B
              printf("value of i is %d\n",i);    \\ C
       }      \\ D
    {
    printf("the value of i is %d\n",i);     \\ E
}

Explanation

  1. The counter variable i is defined at statement A.
  2. The scope of the for loop is up to statement D, which is the end of the for loop for statement B.
  3. If you do not comment out statement E, you will get an error, because you cannot refer i in the outside block.

No comments:

Post a Comment