Article View: comp.lang.c.moderated
Article #2187"extern" and function declarations
From: Lawrence Kirby
Date: Thu, 07 Mar 1996 00:25
Date: Thu, 07 Mar 1996 00:25
95 lines
4554 bytes
4554 bytes
In article <4hgcl2$9e8@solutions.solon.com>
thomas@eel.rdc.toshiba.co.jp "Arun M. Thomas" writes:
> This is the first I've heard of tentative definitions (I'm fairly new at this).
> Why are tentative definitions recognised as being implicitly zero?
C has always required every static and global variable to be initialised to
a known value at program startup. If you provide an explicit initialiser then
that determines the value. If not an implicit initialiser of 0 is used.
Tentative definitions are just the way the ANSI standard states this behaviour
along with the idea that a single object can be defined more than once in a
translation unit so long as all definitions are compatible and there is
never more than one explicit initialisation.
There are some other issues such as type composition (since compatible
types are not necessarily identical types) for instance you can write:
int a[10];
int a[];
and the 2nd becomes the composite of the types int a[10] and int [] which is
int a[10]. You can also write:
int a[];
int a[10];
or
static int a[10];
static int a[];
but you can't write:
static int a[];
static int a[10];
In this case the first line is a definition with internal linkage but
incomplete type (i.e. the type has no defined size). The standard states that
this combination is illegal (but curiously doesn't require a compiler to
diagnose it, although a reasonable one will).
> If the
> value of i in the above example is never initialized, (assuming the last line
> was not present) then it should never be used anywhere. In that case, why
> does one need to bother recognising this as zero?
The point is that it is initialised so code can use the value. It just
isn't initialised explicitly i.e. the compiler supplies the initial value
rather than having it stated explicitly in the source code.
> On a related note, does the concept of declaration vs definition apply to
> variables? Or only to functions? This "tentative definition" strikes me
> as somewhat similar.
Declarations and definitions are applicable to both objects (variables)
and functions. In both cases you can declare an identifier which is simply
a name you can use in the source code to 'access' the object or function.
You can also have a definition for either which is what allocates the
memory for the object or function. For a function the definition is where
you supply the function body within {} . Tentative definitions are not
applicable to functions - you either supply a body or you don't, there is
no default function body supplied by the compiler, which is also why
extern becomes redundant in function declarations/definitions.
> --SNIP--
> : I'm curious - do people find this enlightening (you learnt something),
> : obvious (you knew it already, comp.std.c regulars need not apply),
> : unintelligible (I didn't explain it very clearly) or uninteresting (not
> : worth the bandwidth)?
> :
> I, at least, found this useful :) though I'm still not completely sure I
> understand.
I guess that's what concerns me!
> : I wonder how many C books or courses cover things like tentative
> : definitions (in whatever terminology). K&R2 discusses this in the language
> : reference in appendix A (A10.2) but not, as far as I can tell, in the
> : main tutorial.
>
> I really don't recall ever having run into something like this in my C course
> or in going through various books on C programming. (I have K&R2, but have
> never gone through the appendix word for word.)
In my experience courses can be good for imparting a good general
understanding but the information transfer rate is much lower than reading
a book and they rarely cover the fine detail. The trouble that a lot of
books don't seem to either.
--
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------
Technical Details
Message-ID:
<4hlael$4ed@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: fred@genesis.demon.co.uk (Lawrence Kirby) Newsgroups: comp.lang.c.moderated Subject: "extern" and function declarations Date: 6 Mar 1996 18:25:57 -0600 Organization: none Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4hlael$4ed@solutions.solon.com> Reply-To: fred@genesis.demon.co.uk NNTP-Posting-Host: solutions.solon.com X-Mailer: Demon Internet Simple News v1.27