blogger Indonesia

Follow judhyns blog

FURTHER SCOPE OF VARIABLES

Introduction

In C, you can define the counter in the for loop itself; the counter has scope up to the end of the for loop.

Program

#include  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.

Point to Remember

You can define counter variable inside the for loop.

No comments:

feed

PR Check

activesearchresults

judhyn's blog

 

http://www.judhyn.blogspot.com | |