Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: A question on variable scope, coming from a relative beginner Date: Thu, 12 Nov 2015 17:11:55 -0800 Organization: None to speak of Lines: 41 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="4331"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+jjxtQS6j/jPa55m+Ojbk+" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:ScDG05lbtvuXJy9VtYxCKrt+uY4= sha1:/rhT+4km6JBK1Z/CNbXzEP8YLI4= Xref: csiph.com comp.lang.c:75906 Jon Rossen writes: > Hi all, I'm fairly new to programming and have a question on the C > language with regards to variable scope, compilers and the 'extern' > keyword. I'm using a couple of books that seem to have info that is not > in keeping with my results. Here's a simple little trivial code example > that illustrates the question: > > #include > > int x = 999; > void print_value(void); > > int main(void) > { > extern int x; // This entire statement is not necessary to > // get the proper results, why?? > > printf("%d\n", x); > print_value(); > > return 0; > } > > void print_value(void) > { > extern int x; // This obviously is > printf("%d\n", x); > } > > Result: 999 > 999 The "extern int x;" declaration (not statement) is not necessary either in main or in print_value. The file-scope definition of x is visible in both functions. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"