J
James Kuyper Jr.
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'. 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.
....
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.