K&R App A, par. 8 & 8.2, on Declarat - (nf)

Keith Pilotti pilotti at sdcsla.UUCP
Wed Aug 1 16:15:57 AEST 1984

In article <166 at tellab2.UUCP> thoth at 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.

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 at NPRDC   (ARPA) 

More information about the Comp.lang.c mailing list