According to C, C++ the scope of variables in for loop is within the loop. That is, a variable inside the loop will not be accessible once the loop finishes it job! For example:
for (int i=0;i<MAX;i++) {
// do something in the loop
}
// variable “i” should not be accessible!
But there is another twist in this variable’s scoping. Different compilers should different behaviours in scoping of variables. I will review three compilers here: Borland CPP 3.0, Visual Studio 6.0, GCC 3 and above.
Compiler 1: (Borland C++ 3.0)
This one of the most used compiler by beginners! But I will never recommend it for starting C or C++ as its too old (released in 1989) and have troubles running with latest processors. Now, lets take an example. Consider the loop given below:
for (int i=0;i<MAX;i++) {
printf (“Value of I : %d \n”, i);
}
printf (“Out of loop! \n The value of i is: %d”, i);
Compiler 2: (Visual Studio 6)
Compile 3: (GCC 3 and Above)
Try out the loop in GCC and see how standardized it is. If you don’t use Linux you can GCC based compiler like DEV-C++ for windows platform. For any beginners I would recommend them to study C/C++ using GCC compile and never NEVER use old Borland compilers!
No comments :
Post a Comment