Const objects local to a file by default

L

LJ

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??
 
B

Ben Bacarisse

LJ said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,225
Latest member
Top Crypto Podcasts

Latest Threads

Top