Definition in Header files

J

James Kuyper Jr.

Madhur wrote:
....
I want those variable to be const. Defining them in a C file , I can
not declare them as const.

Yes you can. Have you tried?
Why doesn't C compilation doesn't support Internal linkage as it is
supported by C++??

C does support internal linkage, it just uses different rules for it.

In C an object defined at file scope has external linkage unless
explicitly declared 'static'. In C++, an object declared at file scope
has internal linkage unless explicitly declared 'extern'. As objects
with internal linkage, these constants don't interfere with each other.
You could achieve the same effect in C, without changing the behavior of
a C++ program that #includes the same header, by explicitly declaring
those objects to be static.

However, in C that would be wasteful, because a separate const object
would be created in every translation unit that #includes that header.

In principle, that should also be true in C++. However, there are other
rules in C++ (I don't remember the details), that make it easy for the
implementor to not allocate space for a const object with internal
linkage that is never used, and to in-line the value of a constant if
it's address is never taken. It seems like the as-if rule would allow
the same thing to be done by a C compiler, but some those details that I
don't remember make it easier in C++ than in C.

As a general rule, the best way to do this in C is with macros. Another
alternative is use enumeration constants, but that's usable only for
integer values. I would create actual const objects only when neither of
those techniques would work - the only exception I can think of off hand
is when you want to use aggregate constants, such as structures and
arrays. When you do use that technique, the most efficient way is to
define the object in only one translation unit, and just declare the
object with external linkage in a header file that you #include where
needed. I know you've said you want to avoid it, but that is the right
approach.
 
J

James Kuyper Jr.

user923005 wrote:
....
At any rate, you can have external const in C. The code you posted
should not work in C++ either. Every time you include that header
file, it defines a new instance of the variables.

Yes it should. There's a key difference between C and C++ that makes
this work in C++. In C++, those constants have internal linkage, which
makes it legal.
 
F

fred.l.kleinschmidt

I want those variable to be const. Defining them in a C file , I can
not declare them as const.
Why doesn't C compilation doesn't support Internal linkage as it is
supported by C++??

If they are truly constants, why not just #define them in the include
file
instead of making them const variables? Then the compiler will not
complain about multiple definitions, and they cannot be changed.
 
D

dandonov

On Oct 23, 10:40 am, (e-mail address removed) wrote:

If they are truly constants, why not just #define them in the include
file
instead of making them const variables? Then the compiler will not
complain about multiple definitions, and they cannot be changed.

Well, one reason could be that it's relatively harder to debug
#define's.
Another reason - #define's don't have a type thus they can be easily
misused.

Dimiter
 
C

CBFalconer

dandonov said:
(e-mail address removed) wrote:


cannot be changed.

Well, one reason could be that it's relatively harder to debug
#define's. Another reason - #define's don't have a type thus
they can be easily misused.

Stop and think a moment. These things have values, which are
constant. The type doesn't matter. So you don't care about the
type. Just use:

#define value 123

etc. You can use the result in floating point or integral
expressions.
 
R

Road Tang

Charlie said:
you can only use enum for a constant of type int, and even that can be an
issue because sizeof(enum toto) is not necessarily the same as
sizeof(int). for a double you have to use a macro or a global const
variable. You can make that static, but it may lead to space wastage and
the compiler might complain about static variables being defined but not
used. It would be useful to have a way to tell the compiler that a global
variable
is meant to be used as a constant value. I propose to extend the meaning
of inline for this purpose:

static inline double const PI = 3.14159265358979323846;

That looks good, inline exists in C99
so in C99 compliant compiler,
the combination the static and inline is good way to avoid the non-type
replacement of macro.
Inline is from C++, and is part of method to replace the macro.

Cheers
-Road
 
J

Jack Klein

Madhur wrote:
...

Yes you can. Have you tried?


C does support internal linkage, it just uses different rules for it.

In C an object defined at file scope has external linkage unless
explicitly declared 'static'. In C++, an object declared at file scope
has internal linkage unless explicitly declared 'extern'.

I KNOW you KNOW this, but you could certainly confuse a newbie with
that last sentence, because it is missing a very important word. It
should be:

"In C++, a const object declared at file scope..."
^^^^^

And to get technical, because they get snippy about this over on
comp.lang.c++, there is no such thing as "file scope" in C++. The
phrase is not defined in their standard. It is "namespace scope".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
J

James Kuyper Jr.

Jack said:
I KNOW you KNOW this, but you could certainly confuse a newbie with
that last sentence, because it is missing a very important word. It
should be:

"In C++, a const object declared at file scope..."
^^^^^

And to get technical, because they get snippy about this over on
comp.lang.c++, there is no such thing as "file scope" in C++. The
phrase is not defined in their standard. It is "namespace scope".

Sorry about those errors! As I've said elsewhere, my copy of the C++
standard is currently inaccessible, and I don't get opportunities to
actually use my C++ knowledge as often as I'd like, so my statements
about C++ are less reliable than they should be.
 
D

dandonov

Stop and think a moment. These things have values, which are
constant. The type doesn't matter. So you don't care about the
type. Just use:

#define value 123

etc. You can use the result in floating point or integral
expressions.

You are right. Although in some cases, when you debug a relatively big
project it would be hard to get those #define values. Of course, you
might have a better debugger than the one I am using.
As for the type... Please, forgive me - I am coming from the C++ land
and I do like things to have a type. I might be wrong, though. I am
here to learn.

Dimiter
 
C

CBFalconer

Road said:
Charlie Gordon wrote:
.... snip ...


That looks good, inline exists in C99 so in C99 compliant
compiler, the combination the static and inline is good way to
avoid the non-type replacement of macro.

inline describes functions, not objects. It is meaningless in your
statement above.
 
C

Charlie Gordon

CBFalconer said:
inline describes functions, not objects. It is meaningless in your
statement above.

Of course, this is only a proposal specifying typed constants.
It was unclear whether Road Tang though I was describing the current
standard specification or not.
 
M

Mark McIntyre

I have problems faced in adding definitions in the command header
file. I have defined a header file which includes huge set of global
constants and I am using them in all the C files.

Snip examples.

Declare them in your header using extern.
Define them in _one_ file.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top