There is not much difference between C local variables but there is one important feature that should be noted.
main() { float pi = 3.142; // Usual location for variable definitions cout << "PI is " << pi << endl; int Count = 1; // C++ allows us to place a definition here. while (Count < 10) { cout << Count << endl; } } |
It should be noted that Count is only in scope (usable) from the point of definition and to the end of the block. It can not be used in the previous cout statement.
The idea of this feature is to allow you to put variable declarations near to the place you wish to use them. Personnaly, I feel this should not be neccasary if you write small concise functions.... There is one natty feature of this facility though. Check this example out.
main() { int Last = 10; while (int Count; Count < Last; Count++) { cout << Count << endl; } } |
In this example, Count is defined inside the for statement and only exists while the for block is being executed.
Top | Master Index | Keywords | Functions |