declarations, where? 
Author Message
 declarations, where?

K&R, ANSI C version, says (page 40) "variables must be declared before use".
So, I have variable declarations scattered throughout my source code, but
always above where they are first used.  The code is compiling and running
fine for me,
but some colleagues with different compilers, such as gcc, are having trouble
with it.  Is this something that can likely be fixed by using a certain
compiler flag or option?   Am I wrong in my interpretation of K&R?

I'm using the MINGW32 compiler within the Dev-CPP IDE.

m

--
"Many are stubborn in pursuit of a path they have chosen; few in pursuit of a
goal."
Humans may write to me at this address: zenguy at telus dot net
--



Wed, 12 Oct 2005 01:36:20 GMT  
 declarations, where?

Quote:

>K&R, ANSI C version, says (page 40) "variables must be declared before use".
>So, I have variable declarations scattered throughout my source code, but
>always above where they are first used.  The code is compiling and running
>fine for me,
>but some colleagues with different compilers, such as gcc, are having trouble
>with it.  Is this something that can likely be fixed by using a certain
>compiler flag or option?   Am I wrong in my interpretation of K&R?

>I'm using the MINGW32 compiler within the Dev-CPP IDE.

Note:  My discussion below says "C" when referring to the ANSI C89
standard version of the language.  There is a new version of the language,
ISO C99, which is not yet widely implemented; your K&R2 book refers to
the C89 version, and the compilers you and your colleagues are using
all implement the C89 version.

I'm guessing that you're referring to code of this sort:

int foo(void)
{
        int i = 4 + 3;
        printf("i = %d\n", i);
        int j = i + 9;
        printf("j = %d\n", j);
        return j;

Quote:
}

Alas, this is not allowed by the C standard, and any conforming C
compiler is obligated to reject it.  The correct way to write this
function in C is

int foo(void)
{
        int i = 4 + 3;
        int j;
        printf("i = %d\n", i);
        j = i + 9;
        printf("j = %d\n", j);
        return j;

Quote:
}

Note that all the variables are declared at the beginning of the
function.

Presumably, you are actually using a C++ compiler.  You should probably
stop doing that -- it is likely to cause you other problems, if you
think that you are writing C programs.  (For example, there is a subtle
difference in the definition of ?: , and the usage of casts on void* and
NULL are different.)

I don't have a copy of K&R2 handy, but I am certain that it does tell
you that what you are doing is illegal.  Look at the portion of the book
that describes how a function is laid out; it should say that
declarations must come before the first executable statement.

The grammar at the back (in appendix A, IIRC) also makes this clear, but
it's not exactly an easy read for the layperson.

-andy
--



Thu, 13 Oct 2005 06:28:53 GMT  
 declarations, where?

Quote:

> K&R, ANSI C version, says (page 40) "variables must be declared before use".
> So, I have variable declarations scattered throughout my source code, but
> always above where they are first used.  The code is compiling and running
> fine for me,
> but some colleagues with different compilers, such as gcc, are having trouble
> with it.  Is this something that can likely be fixed by using a certain
> compiler flag or option?   Am I wrong in my interpretation of K&R?

> I'm using the MINGW32 compiler within the Dev-CPP IDE.

During the 3 days since I sent that to CLCM I've found the answers I was
seeking.
Thanks anyway.

Mitchell Timin

--
http://sourceforge.net/projects/annevolve is what I'm into nowadays.
Humans may write to me at this address: zenguy at telus dot net
--



Thu, 13 Oct 2005 06:28:55 GMT  
 declarations, where?

Quote:

> K&R, ANSI C version, says (page 40) "variables must be declared
> before use". So, I have variable declarations scattered throughout
> my source code, but always above where they are first used.  The
> code is compiling and running fine for me, but some colleagues
> with different compilers, such as gcc, are having trouble with it.
> Is this something that can likely be fixed by using a certain
> compiler flag or option?   Am I wrong in my interpretation of K&R?

> I'm using the MINGW32 compiler within the Dev-CPP IDE.

Another provision of C90 says that declarations must precede
executable code.  You are really using gcc extensions, unless you
are flagging it properly with "-W -Wall -ansi -pedantic".

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!
--



Thu, 13 Oct 2005 06:29:02 GMT  
 declarations, where?
Quote:

> K&R, ANSI C version, says (page 40) "variables must be declared before use".
> So, I have variable declarations scattered throughout my source code, but
> always above where they are first used.  The code is compiling and running
> fine for me,
> but some colleagues with different compilers, such as gcc, are having trouble
> with it.  Is this something that can likely be fixed by using a certain
> compiler flag or option?   Am I wrong in my interpretation of K&R?

[...]

Can you give a specific example?  What is the exact error/warning that
the compiler is issuing, and how exactly is the related variable being
declared?

P.S.  Just to let you know that your fake "from" may cause problems for
the sysadmins at the actual "below.net" domain.  (The automated tools
that spammers use to scrape addresses don't know that they're supposed
to "see below".)

--

+---------+----------------------------------+-----------------------------+
| Kenneth |     kenbrody at spamcop.net      | "The opinions expressed     |
|    J.   |    http://www.hvcomputer.com     |  herein are not necessarily |
|  Brody  |      http://www.fptech.com       |  those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
--



Thu, 13 Oct 2005 06:29:14 GMT  
 declarations, where?

Quote:

> K&R, ANSI C version, says (page 40) "variables must be declared before use".
> So, I have variable declarations scattered throughout my source code, but
> always above where they are first used.  The code is compiling and running
> fine for me,
> but some colleagues with different compilers, such as gcc, are having trouble
> with it.  Is this something that can likely be fixed by using a certain
> compiler flag or option?   Am I wrong in my interpretation of K&R?

> I'm using the MINGW32 compiler within the Dev-CPP IDE.

> m

C++ allows them to be scattered.  C does not.  In C all local
declarations must be grouped at the start of a function, before any
executable statement.

--
Scott McPhillips [VC++ MVP]
--



Thu, 13 Oct 2005 06:29:17 GMT  
 declarations, where?

Quote:
>K&R, ANSI C version, says (page 40) "variables must be declared before use".
>So, I have variable declarations scattered throughout my source code, but
>always above where they are first used.  The code is compiling and running
>fine for me,
>but some colleagues with different compilers, such as gcc, are having trouble
>with it.  Is this something that can likely be fixed by using a certain
>compiler flag or option?   Am I wrong in my interpretation of K&R?

>I'm using the MINGW32 compiler within the Dev-CPP IDE.

Without seeing an example, one can only guess. The most likely thing
is that, in C89, declarations may only appear at the beginning of a
block.  In C99, and in C++, declarations may appear most places that a
statement is allowed. It may be that your compiler offers this
extension.

int main ()
{
   int i = 7;   /* ok */
   int n;       /* ok */
   n = i;
   int j;       /* not ok in C89 */
   j = i;
   while (i--) {
      int k;    /* ok */
   }
   return 0;

Quote:
}

--



Thu, 13 Oct 2005 06:29:19 GMT  
 declarations, where?
On 25 Apr 2003 17:36:20 GMT in comp.lang.c.moderated,

Quote:

>K&R, ANSI C version, says (page 40) "variables must be declared before use".
>So, I have variable declarations scattered throughout my source code, but
>always above where they are first used.  The code is compiling and running
>fine for me,
>but some colleagues with different compilers, such as gcc, are having trouble
>with it.  Is this something that can likely be fixed by using a certain
>compiler flag or option?   Am I wrong in my interpretation of K&R?

>I'm using the MINGW32 compiler within the Dev-CPP IDE.

Your usage is valid for C99 and later, not for any earlier
version of the language, which requires all declarations at the
start of a block, after the open brace "{".

Thanks. Take care, Brian Inglis         Calgary, Alberta, Canada
--

    fake address                use address above to reply
--



Thu, 13 Oct 2005 06:31:25 GMT  
 declarations, where?

Quote:
>K&R, ANSI C version, says (page 40) "variables must be declared before use".
>So, I have variable declarations scattered throughout my source code, but
>always above where they are first used.  The code is compiling and running
>fine for me,
>but some colleagues with different compilers, such as gcc, are having trouble
>with it.  Is this something that can likely be fixed by using a certain
>compiler flag or option?   Am I wrong in my interpretation of K&R?

>I'm using the MINGW32 compiler within the Dev-CPP IDE.

In older C compilers (prior to the C99 standard which most compilers
still do not implement), not only did the variables have to be
declared before use, but all the declarations had to be at the
beginning of a block prior to executable statements.  Thus you could
not say
     int a;
     a = 5;
     int b;

Many compilers removed this restriction as a non-standard extension.
If you are taking advantage of such an extension in your compiler but
others either do not have the extension or choose not to activate it,
this could explain the situation you describe.

Some compilers which have non-standard extensions have a way to
deactivate them.  Try to compile your code in "standard only" mode and
see if this is the case.  If not, post a small compilable example that
works on your system and not on others and let the gurus tell you why.

<<Remove the del for email>>
--



Thu, 13 Oct 2005 06:31:30 GMT  
 declarations, where?

Quote:


>> K&R, ANSI C version, says (page 40) "variables must be declared before use".
>> So, I have variable declarations scattered throughout my source code, but
>> always above where they are first used.  The code is compiling and running
>> fine for me,
>> but some colleagues with different compilers, such as gcc, are having trouble
>> with it.  Is this something that can likely be fixed by using a certain
>> compiler flag or option?   Am I wrong in my interpretation of K&R?

>> I'm using the MINGW32 compiler within the Dev-CPP IDE.

>> m
> C++ allows them to be scattered.  C does not.  In C all local
> declarations must be grouped at the start of a function, before any
> executable statement.

That's too general of a statement. C99 allows them.

Alex
--



Sat, 15 Oct 2005 23:45:37 GMT  
 declarations, where?

Quote:


> > C++ allows them to be scattered.  C does not.  In C all local
> > declarations must be grouped at the start of a function, before any
> > executable statement.

> That's too general of a statement. C99 allows them.

It's too general for C89 too: declarations may appear at the beginning
of any compound statement, not just a function body.

-- [mdw]
--



Sun, 30 Oct 2005 05:10:44 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. defining declarations vs. referencing declarations

2. C Declaration --> VB Declaration

3. Compiler error C2244 (unable to match definition to declaration) - template class function

4. Function declaration/definition match bug

5. Nondefining declaration of __gc class

6. Declaration problem with __gc[] & a MemoryStream

7. missing libraries/declarations?

8. TLB and declarations

9. Pointer declaration

10. /Wp64 and declaration/defintion mismatch

11. unresolved type declaration in included ATL DLL

12. function of a function and variable declaration...

 

 
Powered by phpBB® Forum Software