[comp.lang.c] declaring variable

bagpiper@oxy.edu (Michael Paul Hunter) (04/06/89)

A friend of mind had a program that wasn't working properly. (what a nice start)
Within this program was a declaration that was similar to:

double A,
       B,
       fooY,
       fooX,
       whelp; /* <--- note this */
       WhatAmI,
       AndMoreVariables;

He was compiling his program under TurboC 2.0.	The compiler with all warning
turned on was not saying anything about the semi after whelp.  But It did
not appear to be allocating space for the variable....at least in the debugger
if there was a line of the form whelp=constant, whelp did not change
value.

Two questions:
    1) Why isn't WhatAmI, AndMoreVariables; a syntax error?  How does the
       grammar produce this?
    2) What happens on other machines....do most compilers say something
       and does lint howl?


				      Thank you,
					 Michael Hunter
					 bagpiper@oxy.edu

hascall@atanasoff.cs.iastate.edu (John Hascall) (04/06/89)

In article <26707@tiger.oxy.edu> bagpiper@oxy.edu (Michael Paul Hunter) writes:
>A friend of mind had a program that wasn't working properly. (what a nice start)
>Within this program was a declaration that was similar to:

>double A,
>       B,
>       fooY,
>       fooX,
>       whelp; /* <--- note this */
>       WhatAmI,
>       AndMoreVariables;

   It looks like what we have here is:  

       double A....whelp;

       WhatAmI, AndMoreVariables;
       ^      ^       ^
       expr   comma   expression
	      operator

   Which appears to be a valid (although non-useful) statement.

Any other guesses?

John Hascall

dmg@ssc-vax.UUCP (David Geary) (04/08/89)

In article <26707@tiger.oxy.edu>, Michael Hunter writes:

|| A friend of mind had a program that wasn't working properly. (what a nice start)
|| Within this program was a declaration that was similar to:

|| double A,
  ||      B,
||        fooY,
||        fooX,
||        whelp; /* <--- note this */
||        WhatAmI,
||        AndMoreVariables;
|| 
|| He was compiling his program under TurboC 2.0.	The compiler with all warning
|| turned on was not saying anything about the semi after whelp.  But It did
|| not appear to be allocating space for the variable....at least in the debugger
|| if there was a line of the form whelp=constant, whelp did not change
|| value.

|| Two questions:
||     1) Why isn't WhatAmI, AndMoreVariables; a syntax error?  How does the
||        grammar produce this?
||     2) What happens on other machines....do most compilers say something
||        and does lint howl?

   One answer:

   TurboC2.0 is broken in this regard.

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ David Geary, Boeing Aerospace, Seattle                 ~ 
~ "I wish I lived where it *only* rains 364 days a year" ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

fieland@hobbiton.prime.com (04/08/89)

I tried your example with the Sun C compiler --
it quite nicely complains,
   "WhatAmI undefined"  and
   "AndMoreVariables undefined"


Peggy

scs@adam.pika.mit.edu (Steve Summit) (04/08/89)

In article <26707@tiger.oxy.edu> bagpiper@oxy.edu (Michael Paul Hunter) writes:
>A friend of mind had a program that wasn't working properly.
>Within this program was a declaration that was similar to:
>double A,
>       B,
>       fooY,
>       fooX,
>       whelp; /* <--- note this */
>       WhatAmI,
>       AndMoreVariables;

I see this style often, and I don't particularly like it.
Besides the potential for the error illustrated, this style
reflects a bit of compulsive ordering which accomplishes nothing.
I can think of three potentially valid ways of ordering
declarations: alphabetically, in order of use, or grouped in some
kind of functional categories.  Grouping variables by type
accomplishes nothing.

(Think about it.  Aside from the information they impart to the
compiler, declarations can be read by people to determine needed
information.  Usually, the needed information is a variable's
type, so ordering the declarations by type, i.e. the unknown, is
backwards, sort of like sorting a book's index by page number.)

Grouping declarations in this way also makes it hard to perform
isolated modifications.  Suppose, for example, that the variable
"A" is to be conditionally compiled out.  As it stands, I can't
just slap an #ifdef around it:

	#ifdef needvariableA
	double A,
	#endif
	       B,

Instead, I have to insert an extra "double" in front of B, making
it show up unnecessarily in a diff listing.  I also have to
change A's trailing comma to a semicolon, for the day when it
gets compiled in again.

(This is not a forced example; it came up several times when I
was getting kermit to fit on a non-split-I&D pdp11 by #ifdeffing
out huge chunks of it.  I'll admit that it's a fine point, but
style often revolves around fine points.)

Before you chide me for complaining about having to change a
comma to a semicolon in my #ifdef example, I'll point out that
this sort of thing probably led to the original bug: someone
added WhatAmI and AndMoreVariables without remembering to change
the semicolon after whelp to a comma.

I find it useful to place each declaration on its own line, with
its own type-specifier, unless the variables are very closely
related.  That is, I'd write

	int apples;
	int oranges;

only using commas in an example like

	int x, y;

where x and y are the cartesian coordinates of a single point.

When each declaration stands alone, you can add, delete,
rearrange, #ifdef, etc. to your heart's content, without annoying
syntax errors.

No complaints about the extra keystrokes involved in repeating
the type-specifiers, please.

                                            Steve Summit
                                            scs@adam.pika.mit.edu

Devin_E_Ben-Hur@cup.portal.com (04/09/89)

>In article <26707@tiger.oxy.edu>, Michael Hunter writes:
>| Within this program was a declaration that was similar to:
>| double A,
>|        B,
>|        fooY,
>|        fooX,
>|        whelp; /* <--- note this */
>|        WhatAmI,       /* line 6 */
>|        AndMoreVariables;
>|
>| He was compiling his program under TurboC 2.0.The compiler with all warning
>| turned on was not saying anything about the semi after whelp.  But It did
>| not appear to be allocating space for the variable..at least in the debugger
>| if there was a line of the form whelp=constant, whelp did not change
>| value.
>
>| Two questions:
>|     1) Why isn't WhatAmI, AndMoreVariables; a syntax error?  How does the
>|        grammar produce this?
>|     2) What happens on other machines....do most compilers say something
>|        and does lint howl?
>
>   One answer:
>
>   TurboC2.0 is broken in this regard.
>
>--
>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>~ David Geary, Boeing Aerospace, Seattle                 ~
>~ "I wish I lived where it *only* rains 364 days a year" ~
>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Horsepucky... here's TurboC 2.0's responce to the above input:
 
 Turbo C  Version 2.0  Copyright (c) 1987, 1988 Borland International
 t.c:
 Error t.c 6: Declaration needs type or storage class
 *** 1 errors in Compile ***
 
         Available memory 393548
 
The original poster is either blind or the variables WhatAmI, AndMoreVariables
are actually declared names (outside of the scope of the posted fragment)
such that:                              WhatAmI, AndMoreVariables;
parses as a C comma operator statement: <expr>, <expr> ;

Devin_Ben-Hur@Cup.Portal.Com
...ucbvax!sun!portal!cup.portal.com!devin_ben-hur

Kevin_P_McCarty@cup.portal.com (04/10/89)

--test1.c----------
    double  A,
            B,
            fooY,
            fooX,
            whelp;
            WhatAmI,
            AndMoreVariables;
main() { }
--test2.c----------
main()
{
    double  A,
            B,
            fooY,
            fooX,
            whelp;
            WhatAmI,
            AndMoreVariables;
}

tcc -c -A test1
Turbo C  Version 2.0  Copyright (c) 1987, 1988 Borland International
test1.c:
Error test1.c 6: Declaration needs type or storage class
Warning test1.c 10: Function should return a value in function main
*** 1 errors in Compile ***

tcc -c -A test2
Turbo C  Version 2.0  Copyright (c) 1987, 1988 Borland International
test2.c:
Error test2.c 8: Undefined symbol 'WhatAmI' in function main
Error test2.c 9: Undefined symbol 'AndMoreVariables' in function main
Warning test2.c 8: Code has no effect in function main
Warning test2.c 9: Code has no effect in function main
*** 2 errors in Compile ***

No, Turbo-C 2.0 isn't broken, something else is.
Is WhatAmI a C keyword, perhaps?  Grep will help you find out if
it's been defined or declared elsewhere.

Kevin McCarty

chris@mimsy.UUCP (Chris Torek) (04/11/89)

In article <16969@cup.portal.com> Kevin_P_McCarty@cup.portal.com writes:
[referring to the `declaration' sequence `double A, ...; WhatAmI, ...']
>tcc -c -A test1
>Turbo C  Version 2.0  Copyright (c) 1987, 1988 Borland International
[errors]

I do not recall whether the original question referred specifically
to Turbo C version 2.0.  In any case, it is worth noting that in some
Classic C compilers, naming a variable outside a function without
giving a type declares that variable as an integer.  Thus:

	nwords;
	main()
	{
		...

produces the global integer variable `nwords'.

This was not legal in the dpANS when last I checked (which was before
it became a pANS).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

hascall@atanasoff.cs.iastate.edu (John Hascall) (04/11/89)

In article <16969@cup.portal.com> Kevin_P_McCarty@cup.portal.com writes:
>--test1.c----------
>    double  A,
>            B,
>            fooY,
>            fooX,
>            whelp;
>            WhatAmI,
>            AndMoreVariables;

   To quote from K&R, appendix A, section 8.2, "Type Specifiers":

       If the type-specifier is missing from a declaration, it
       is taken to be int.

   This seems like one "feature" we could afford to lose (IMHO).

   How, (I'm sure you'll tell me :-), do you (does the compiler) tell:

                 WhatAmI , AndMoreVariables ;
                    |    |      \
                  expr. comma   expression
                       operator

   from:

                 WhatAmI , AndMoreVariables ;
            |       |    |      \
         assumed  ident comma  identifier
	 type int

  John Hascall / ISU Comp Center

carlp@frigg.iscs.com (Carl Paukstis) (04/12/89)

>In article <26707@tiger.oxy.edu> bagpiper@oxy.edu (Michael Paul Hunter) writes:
>A friend of mind had a program that wasn't working properly. (what a nice start)
>Within this program was a declaration that was similar to:

>double A,
>       B,
>       fooY,
>       fooX,
>       whelp; /* <--- note this */
>       WhatAmI,
>       AndMoreVariables;

My guess is that the compiler allows the older style, where the word
"int" was optional and assumed in a variable declaration.  WhatAmI
and AndMoreVariables would be "int".

Maybe non-obvious, but quite common in some older programs.
--
Carl Paukstis       "I'm the NRA"         |  DOMAIN:   carlp@iscuva.ISCS.COM   
    "Just say NO to the 'War on Liberty'" |  UUCP:     ...uunet!iscuva!carlp  
                                          |  GEnie: carlp         BIX: carlp
I speak for myself, not my employer.      |  Ma Bell:  +1 509 927 5600 x5321