Re: Defining variable in C header file related doubt



whirlwindkevin <manumg007@xxxxxxxxx> wrote:

I saw a program source code in which a variable is defined in a header
file and that header file is included in 2 different C files.When i
compile and link the files no error is being thrown.How is this
possible.I thought it will throw "Variable redefinition Error". Giving
the source code for reference.Please help me out on this...

Having multiple definitions results in undefined behavior -- the
implementation is not required to diagnose it, and many (if not most)
implementations will not, particularly if the definitions are compatible
and the variable is not explicitly initialized. The code is still
wrong: the header should have only a declaration of the variable (with
extern) and exactly one source file should have the actual definition.

-- Larry Jones

OK, what's the NEXT amendment say? I know it's in here someplace. -- Calvin
.



Relevant Pages

  • Re: Function prefix comments in C files
    ... > function prototype in a header .h file. ... I work for a large organization where as 10 or so programmers might ... directory at the source code to find information about how to call a ...
    (comp.arch.embedded)
  • Re: insert dialog
    ... > 3 Use wizard to Create new class CSecondDialog, ... > //create the second dialog window ... > source code in the #include area with others, ... > 6 Include the header for the main FIRST dialog in the Seconddlg.cpp source ...
    (microsoft.public.vc.mfc)
  • Re: header files including other files
    ... Really I guess I am asking advice about source code organisation. ... Now, in any particular module, the module header file declares exported ...
    (comp.arch.embedded)
  • Declarations and Definitions in One Header
    ... Declarations and Definitions in One Header ... C++ object definitions can be quite complex. ... In principle, your source code ...
    (comp.lang.cpp)
  • Re: [expert advice required][Is getImageSize really smart ?]
    ... >> From a quick glance at the source code, it looks like it should be bailing out ... >> once it's got all the header, and shouldn't have to fetch the whole image. ... >Tanks Andy for this test: ...
    (comp.lang.php)