473,763 Members | 3,855 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const and extern...

When I read about volatile keyword....whil e explaning there was an
declaration like...
extern const a;

But as a rule const shoule be initialised where it is defined...but we
do ot initialise extern where we declare it....So what is exactly
happening here?

May 27 '07 #1
2 2907
Shraddha wrote:
When I read about volatile keyword....whil e explaning there was an
declaration like...
extern const a;

But as a rule const shoule be initialised where it is defined...but we
do ot initialise extern where we declare it....So what is exactly
happening here?
It's being declared. It must be defined once in the application.

--
Ian Collins.
May 27 '07 #2
In article <11************ **********@q19g 2000prn.googleg roups.com>
Shraddha <sh************ *@gmail.comwrot e:
>... there was an declaration like...
extern const a;
But as a rule const shoule be initialised where it is defined...
Yes.
>but we do not initialise extern where we declare it....So what
is exactly happening here?
This is, quite simply, not a definition.

In C, when you name a variable's existence, you can *define* the
variable:

int foo = 42;

or merely *declare* the variable:

extern float bar;

If you define it, this also declares it (every definition is also
a declaration), but if you merely declare it, you do not define it.

For variables declared outside a function, the declaration is also
a definition if the variable is initialized, but is normally just
a declaration if the "extern" keyword is used. (If you do both --
write the "extern" keyword, then initialize it anyway -- you get
a definition, so:

extern int foo = 42;

means the same thing as the first definition above.)

There is a third state, sort of between the two, called a "tentative
definition". You get this when you omit the "extern" keyword, but
also omit the initializer:

char *tentative;

In this case, the variable is "tentativel y defined" from that point
forward in the translation unit, up until the compiler sees a
(non-tentative, for-sure for-real) definition:

char *zorg; /* initially, tentative */
char *zorg = "evil"; /* now, defined */

or the translation unit ends. If the translation unit ends and no
definition has occurred, the effect is the same[%] as if you had added,
just at the end, an actual definition with an initializer of {0}:

char *tentative;
/* if the file ends, now, this means:
char *tentative = {0};
which makes it initially NULL. */

Again, these rules apply ONLY to file-scope variables (those outside
function blocks). Inside a function, there is no such thing as a
"tentative definition":

void f(void) {
int x; /* actually defines x, which contains no useful value,
so be sure to give it one before using it */
}

-----
[%] In some implementations , "tentative definition" variables are
treated specially, and multiple tentative definitions in separate
translation units are simply merged at link time. The C Standards
(C89 and C99 both) say that this situation is undefined, so
implementations can define it, if they like. Other implementations
make it an error. One popular implementation (GCC) can be told
to behave either way.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
May 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
10223
by: William Payne | last post by:
Hello, in my application (which spans approximately 20 source files and a 2-3 thousand lines) I have a few global varialbes, variables that never change their values. One of them is declared like this: /* globals.h */ extern const int g_tray_icon_callback; /* globals.cpp */ const int g_tray_icon_callback = 4711; Now, when I tried to use that variable as a case label in switch statement,
4
9496
by: Steven T. Hatton | last post by:
#include <iostream> namespace ns{ const char name = "This is a Class Name";//won't compile //char name = "This is a Class Name"; // compiles template <typename T, char* Name_CA=name> struct A { A() : _data(15)
9
10274
by: Mathieu Malaterre | last post by:
Hello, This thread follow my previous one on the gcc mailing list. Basically I -still- have a problem in my code. I define in a header file: static const std::string foo = "bar"; Which not only seems to be a bad practice but also creates a bug on MacOSX panther (gcc 3.3).
3
10657
by: Rolf S. Arvidson | last post by:
Be kind, a newb question here. I don't understand why I get a compile-time error from the following: I define a variable in main.cpp as such double x = 1.; int _tmain(int argc, _TCHAR* argv){ ...; }
9
5119
by: C. J. Clegg | last post by:
When you say "const int FOO = 0" (as the commonly-recommended C++ alternative to "#define FOO 0"), isn't that declaration globally visible? I have "const int FOO = 0;" in one source file and "extern int FOO;" in another source file (also tried "extern const int FOO;") and at link time, I get undefined references to FOO. Removed the "const" from the FOO declaration and everything worked fine.
5
1936
by: gw7rib | last post by:
I was having linking errors when I put: const LPCTSTR Main_window_name = _TEXT("Thingy_main_window"); in one file and extern const LPCTSTR Main_window_name; in another. I've since realised that this is because (in C++) consts do not, by default, have external linkage. I've solved the problem by
2
1710
by: PlayDough | last post by:
I think this is something about C++ that I don't understand, and in versions of g++ prior to 4.1.1, hasn't been a problem. Say I have this basic file: test.cpp: const char test_str = "abcd"; And I compile this. I'd expect test_str to be exported so other modules can use it. So, I do nm (for GNU) and dumpbin (for Visual
10
5974
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it is unused, I know that it does not take up storage in the
13
2846
by: S James S Stapleton | last post by:
I have some code, and I want to make it future-resistant. I have a bunch of variables that are set up run-time, and once set up, should act as constants. I don't want to #define them, because their values are determined during the execution of the initialization routines, so I made them const (I'd rather making it hard for a dev to accidentally overwrite the values). The problem is, they are being set run-time by the return of a function...
21
2759
by: Christian Meier | last post by:
Hello NG I have the following code: file1.h: static const int iValue = 5; <EOF>
0
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9389
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10149
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10003
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9828
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7370
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5410
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2797
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.