# C++ Local Variables.

There is not much difference between C local variables but there is one important feature that should be noted.

- Variable definitions are now considered to be executable statements.

This means that you can now put variable definitions anywhere in the code and not jut at the start of a block. Here is an example.

        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.

## Examples:

## See Also:

## C References

C local variables

data types.

Top | Master Index | Keywords | Functions