[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Const objects local to a file by default

LJ

7/22/2011 5:52:00 PM

I read that in C++ const objects are local to a file by default that
is if extern is not specified, is this thing also valid in C??
3 Answers

blp

7/22/2011 6:28:00 PM

0

LJ <djtheshowstopper@gmail.com> writes:

> I read that in C++ const objects are local to a file by default that
> is if extern is not specified, is this thing also valid in C??

No.
--
"I should killfile you where you stand, worthless human." --Kaz

TheGunslinger

7/22/2011 10:00:00 PM

0

On Fri, 22 Jul 2011 11:27:35 -0700, blp@cs.stanford.edu (Ben Pfaff)
wrote:

>LJ <djtheshowstopper@gmail.com> writes:
>
>> I read that in C++ const objects are local to a file by default that
>> is if extern is not specified, is this thing also valid in C??
>
>No.

Purely a suggestion, review pages 286+ in Sam's Teach Yourself C, 6th
Edition.

Also, pages 453-463 in C Primer Plus, 5th Edition.

These references will give you a good starting point for scope of
variables, use of 'const' and 'extern' keywords.

IMHO,

MJR

Ben Bacarisse

7/23/2011 1:06:00 AM

0

LJ <djtheshowstopper@gmail.com> writes:

> I read that in C++ const objects are local to a file by default that
> is if extern is not specified, is this thing also valid in C??

I think you are mixing up several concepts that should be kept separate
-- at least I think it helps to be clear about the terms. Names in C
have both scope and linkage whereas objects have only a lifetime. The
scope determines what portions of the program text may refer to a given
name, and the linkage determines whether two (or more) names refer to the
same (or different) objects.

A declaration outside of any functions gives the name file scope. In C,
it will have external linkages unless the declaration includes the
static keyword. Thus, an objects defined by "const int x = 42;" can be
referred to from another translation unit using "extern const int x;".
Both instances of the name ("x") have external linkage and therefore
refer to the same object. Had the declaration been "static const int x
= 42;" the name would have internal linkage and could not be referred to
from another translation unit (except, of course, indirectly by using a
pointer).

This is different to C++, where a name declared "const" has internal
linkage by default.

--
Ben.