[Tutor] Help

Alan Gauld alan.gauld at btinternet.com
Tue Nov 1 18:53:02 CET 2011

On 01/11/11 04:10, Chris Kavanagh wrote:

>> before. In other languages, ala C++, don't global variables have to be
>> declared at the 'top' of the code??

No, that's just common usage.
You can declare a variable anywhere in C/C++ provided it's before
it is used. But that can lead to hard to read code (hunting for variable 
definitions to see their type, say). One place where it is common to 
define variables away from the top of yourcode is in a for loop:

for (int i = 0;i<10;i++){
      // pass

printf("%d\n", i);

is valid C++(*) and defines the loop variable i inside the loop 
condition, then uses it after the loop.

But apart from that its not that common.

(*)Oops, I just tried it and got a obsolescence warning from g++ so
that may have changed recently! But it worked ok with -fpermissive 


Alan G.

More information about the Tutor mailing list