delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/21/02:39:50

Date: Tue, 21 Jan 1997 09:25:39 -0600 (CST)
From: Adrian Oboroc <ash AT cinf DOT usm DOT md>
To: djgpp AT delorie DOT com
Subject: C & C++ variable definitions
Message-ID: <Pine.LNX.3.93.970121092428.6000F-100000@cinf.usm.md>
MIME-Version: 1.0

Michael Phelps <morphine AT hops DOT cs DOT jhu DOT edu> wrote:

>On Sat, 18 Jan 1997, Daniel Boyer wrote:
>
>> Ok, can somebody please tell me what is wrong with this line of code:
>>
>>       for(char SIX = 'a'; SIX < '}'; SIX++)
>
>It looks like you're trying to declare a variable within the for
>statement.  Change the file extension to ".cc" and compile it with gxx to
>use C++, which allows such constructs.  I don't think C allows this
>(though I could be wrong, and I know it _does_ allow declaring variables
>within a "block").

No, Michael, you are right. ANSI C lets you define variables *only* in
the beginning of a block, before all operators. IE, will be right:

        char SIX;
        for(SIX = 'a'; SIX < '}'; SIX++)
        {
          ...
        }

But completly wrong (for ANSI C, not for C++):

        for(char SIX = 'a'; SIX < '}'; SIX++)
        {
          ...
        }

But it's all right with something like:

        char SIX;
        for(char SIX = 'a'; SIX < '}'; SIX++)
        {
          ...
        }
        {
                int SEVEN;
        }
        
because SEVEN was defined in a beginning of the block, limited with brackets,
no metter, thet "for" statement is plased before.

                        CU, AsH
                        

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019