c - The extern class specifier -



c - The extern class specifier -

if have external variable this:

int a;

is right declaration equals to:

extern int = 0;

and not equals to:

extern int;

is same local variable write this:

int local;

is equals to:

auto int local;

in both cases compiler put, default, specifier extern or auto?

the rules linkage , external definitions little odd, partly historic reasons (cf. illustration this c89 rationale). rules objects (they different function declarations) are:

at file scope:

auto , register not allowed, diagnostic required1). static means internal linkage2) , declaration (tentative) definition3). extern means linkage declared, if none declared, external linkage4). no storage-class specifier means external linkage5) , declaration (tentative) definition6). if multiple declarations disagree on linkage, behaviour undefined7) (gcc aborts compilation, compilers, guess).

a tentative definition means, object defined in same translation unit, if there no explicit definition, 0 assumed.8) multiple explicit definition constraint violation identifiers internal linkage; identifiers external linkage, behaviour undefined (gcc aborts compilation).9)

the first 2 examples mostly, not equivalent.

int a;

a has external linkage. declaration tentative definition; if no explicit initialization follows, initialized 0. if there declaration static int a;, behaviour undefined.

extern int = 0;

this, on other hand, declares a have external linkage if either external declaration visible or none visible. if followed static declaration of a, behaviour undefined; if preceeded one, linkage internal. declaration initializes a 0, no other explicit definition allowed appear in translation unit.

extern int a;

(i assumed extern int a; instead of extern int;. if meant latter: that’s constraint violation.)

this (external, if no other declaration visible) declaration without definition. usually, definition appears in translation unit. must appear somewhere if identifier used in look evaluated.

at block scope:

extern means same @ file scope, except there no tentative definition. no initialization allowed.10) static means no linkage (but static storage-duration).11) if there no initialization, initialized 0 (once, @ programme startup).12) auto or none means no linkage. if there no initialization13), value of variable indeterminate, reading such value undefined (not exactly, practically). register same auto, it’s constraint violation take address. no re-declaration allowed identifiers (denoting object) no linkage.14)

thus, lastly 2 examples equivalent.

the 2 storage-class specifiers not mentioned here typedef , (since c11) _thread_local.15)

references c11 (n1570):

1) 6.9 p2. 2) 6.2.2 p3. 3) 6.9.2 p2. 4) 6.2.2 p4. 5) 6.2.2 p5. 6) 6.9.2 p2. 7) 6.2.2 p7 8) 6.9.2 p2. 9) 6.9 p3 , p5. 10) 6.2.2 p4 , 6.7.9 p5. 11) 6.2.2 p6. 12) , 13) 6.7.9 p10. 14) 6.7 p3. 15) cf. 6.7.1.

c

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -