[net.lang.c] K&R App A, par. 8 & 8.2, on Declarat

jab@uokvax.UUCP (06/30/84)

#R:log-hb:-18000:uokvax:3000030:000:1343
uokvax!jab    Jun 30 10:30:00 1984

/***** uokvax:net.lang.c / log-hb!hans /  6:49 pm  Jun 27, 1984 */
[]
In K&R App A, para 8, it is stated
declaration:
	decl-specifiers declarator-list(optional);
and
decl-specifiers:
	type-specifier decl-specifiers(optional);
	sc-specifiers decl-specifiers(optional);

In para 8.2 it is stated: If the type-specifier is missing from a
declaration, it is taken to be int.

(The author then goes on to ask about assumed types.)
/* ---------- */

In the movie "Private Benjamin," a recruiter promises JAP Judy Benjamin a
three year vacation in the army. "Every soldier gets his/her own condo"

After so long, it becomes apparent that he wasn't giving her the full story.
When Judy later says something to the effect of "but my recruiter said..."
she is told "do you still believe your recruiter?"

There's an analogy here. The K&R book is fine as a tutorial, but the Reference
Manual in the back is not rigorous enough for questions such as the above. The
language specifications don't really exist: I pity anyone trying to write a C
compiler that isn't based on PCC, where your obvious out is "but that's the way
it was done in the version of the compiler I started with."

And to think, there was one class I saw where a professor asked the students for
a YACC specification of C, starting from the C Reference Manual.

	Jeff Bowles
	Lisle, IL

hans@log-hb.UUCP (07/04/84)

[]
About K&R not being rigorous enough, I beg to differ!
In this particular case, that is about assumed types, my own interpretation
is that K&R states clearly that 
a;
is no legal declaration, but
int a;
or
static a;
are.
I was merely trying to get other opinions.
-- 
			{decvax,philabs}!mcvax!enea!log-hb!hans
			Hans Albertsson, 
			TeleLOGIC AB
			Box 1001,
			S-14901 Nynashamn,
			SWEDEN

kah@hpfclq.UUCP (07/08/84)

Re: whether 
	a =1;
can be a legal declaration.

See K&R, Appendix A, 18.3 and 18.4.  Note that the "declaration-list"
productions you reference refer to statements -- ie, declarations
within a function-definition.  And here it is necessary to have either
an sc-specifier or a type-specifier in order to distinguish a declaration
statement from an expression statement.

However, at the global level, declarations are data-definition's (see 18.4),
and here both the type-specifier and (extern | static) are optional.
So
	x;
	y = 1;
are legal external data-definitions
and are equivalent to
	int x;
	int y = 1;
respectively.

K.Harris
hpfcla!kah

hans@log-hb.UUCP (Hans Albertsson) (07/12/84)

[]
Yes, you're quite right, I missed the distinction, odd.

Actually, the correct ref. is App. A, par. 10.
18 is explicitly stated to be "guidelines rather than rules".

Alright, then, I stand corrected....
( Not that I LIKE the 
	a;
  form.. )
-- 
			{decvax,philabs}!mcvax!enea!log-hb!hans
			Hans Albertsson, 
			TeleLOGIC AB
			Box 1001,
			S-14901 Nynashamn,
			SWEDEN

gwyn@BRL-VLD.ARPA (07/26/84)

From:      Doug Gwyn (VLD/VMB) <gwyn@BRL-VLD.ARPA>

Rong, the `a' inside main() WAS the global variable.

jim@ism780b.UUCP (08/01/84)

#R:log-hb:-18000:ism780b:25500002:000:686
ism780b!jim    Jul 13 19:35:00 1984

> I agree that "a = 1;" should be a valid global declaration, but see
> nothing wrong with it as a local one.  The statements "int a=1;"
> "int a; a = 1;" and "a = 1;" should all produce equivalent code!
> This is because of the way C handles initialization of automatic
> local variables.  The only problem I can see is in letting the
> compiler tell where the declarations end and the statements begin.
> If you allow this as a valid declaration, that's not a problem.

int a = 2;
main() {
	a = 1;

	x();
}
x() {
	printf("%d\n", a);
}

will print "2" if "a = 1" is a declaration,
"1" if it is an assignment (current, expected, behavior).

-- Jim Balter, INTERACTIVE Systems (ima!jim)

pilotti@sdcsla.UUCP (Keith Pilotti) (08/01/84)

In article <166@tellab2.UUCP> thoth@tellab2.UUCP (Marcus Hall) writes:
>>int a = 2;
>>main() {
>>	a = 1;
>>
>>	x();
>>}
>>x() {
>>	printf("%d\n", a);
>>}
>>
>>will print "2" if "a = 1" is a declaration,
>>"1" if it is an assignment (current, expected, behavior).
>
>No, the reference to 'a' in x() is to the global 'a'.  The 'a' that is inside
> main() can only be referenced inside main or by passing the address to another
> function.
>
>marcus
>..!ihnp4!tellab1!tellab2!thoth

There IS NO 'a' inside main(), that is the point of the original article.
The 'a = 1' is an assignment to the GLOBAL 'a', and the program prints '1'.

(Tested on C compilers on both VAX (4.2BSD) and Sun Workstation (1.1))

___________________________________________________________
KEITH F PILOTTI -- UC SanDiego Cognitive Science Laboratory
     ...{philabs,ucbvax}!sdcsvax!sdcsla!pilotti  (UUCP) 
                                 pilotti@NPRDC   (ARPA)