Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C C#
I have a header file header.h which contains
int a=10
It a is defined and declared in Header . I know only declaration takes place inside Header file
 
Source file where header.h is referenced. If i put a
printf("%d",a); 
It prints 10 . Will variable be created in source how this works? I wish to know how this basic works .
Posted 28-Jan-13 21:48pm
Edited 28-Jan-13 23:28pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Header files are separate parts of the course code, in the same way that you can have two separate .C files and combine them into a single program. In theory, you can take everything in a .H file, and paste it directly into the .C file and it will work exactly the same. We don't do that, because it makes the .C file big and cluttered, and it makes it difficult to use the same headers in different source files. For example, if you define a constant in your code which is the maximum number of FooBar objects you can handle, then declaring it in a .H file means that the functions which call you code from different .C file can use the same constant and always get the same value as you do.
 
If you declare it in your code and copy it into a different file then it won't get updated if you need to half it for some reason, and then things start to fall apart and code breaks for no obvious reason.
 
If you declare variables in a .H file, then that is the same as declaring them in your .C file, outside the functions, so as global variables.
 
Personally, I don't declare variables in .H files - simply because it is dangerous - if you use the .H in two places, you will get two different variables with the same name, and it isn't obvious that they are not the same value.
  Permalink  
Comments
Argonia at 29-Jan-13 3:43am
   
I have a question. Lets say you have some global variables some consts for example the maximum range of few arrays and so on . Where will you declare them in which .h file to be filled the encapsulation rule of oop and what about using "#pragma one" in headers ?
OriginalGriff at 29-Jan-13 3:50am
   
If you are talking about OOPs then I wouldn't declare them as "loose" objects in a header fiel at all - I would make them constant values as part of the class that uses them.
 
Global variables I would put in a separate GLOBALS.C file (not a .H files at all) to make absolutely sure that there is one and only one definition of each.
 
I personally don't use "#pragma once" as it is non-standard and thus not available in all the systems I might have to use. If there are other ways which are better practice and standard, then I use them. (I don't know where I might what to use this code tomorrow!)
Argonia at 29-Jan-13 4:03am
   
You are telling me that the globals should be declared in the source file not the header or there is a trick about adding .c file with globals into c++ projects?
OriginalGriff at 29-Jan-13 4:17am
   
Yes. Source, not header.
Argonia at 29-Jan-13 4:21am
   
Thank you
OriginalGriff at 29-Jan-13 4:27am
   
You're welcome!
Edo Tzumer at 29-Jan-13 4:27am
   
5+
CPallini at 29-Jan-13 4:53am
   
5.
steven8Gerrard at 29-Jan-13 7:25am
   
Thanks for the reply. Much appreciated
OriginalGriff at 29-Jan-13 7:26am
   
You're welcome!
H.Brydon at 30-Jan-13 2:36am
   
I think you are confusing "#pragma once" with "__declspec(selectany)".
 
The #pragma once directive tells the compiler to not include the entire header file if it has been already included by another reference. This seems to be available on more than one platform but I don't know specifics beyond the Microsoft compilers.
 
The __declspec(selectany) spec is Microsoft-only and tells the linker to link the particular symbol into the execution unit using any copy of the symbol that it chooses. This can be used if you have something multiply defined (like you likely would if it is defined in a header file).
 
The 2 concepts are orthogonal to each other, but I suspect from context that you stated the former and meant the latter.
OriginalGriff at 30-Jan-13 3:25am
   
No, the #pragma once is what I meant - it is available on more than one platform, but not on all.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Due to archaic C++ technology based on header files and its linking, even such trivial thing is not that simple.
If you define (not just declare) a variable in a header file, this declaration will be repeated whenever this file is included, which will be treated as an attempt to create more than one object.
 
Therefore, the definition should be only one, in a single C file; and in header file it should be referenced as extern.
 
This is discussed in many places. Please see, for example, here: http://stackoverflow.com/questions/2868651/including-c-header-file-with-lots-of-global-variables[^].
 
First of all, I would advise to avoid global variables by all means. If you still need some, I would advise to avoid such situation and rely on classes whenever possible. You can find some sample in one of the answers referenced above.
 
—SA
  Permalink  
Comments
Edo Tzumer at 29-Jan-13 4:26am
   
5+
Sergey Alexandrovich Kryukov at 29-Jan-13 6:13am
   
Thank you, Edo.
—SA
CPallini at 29-Jan-13 4:55am
   
5, in spite of 'archaic C++ technology' :-)
Sergey Alexandrovich Kryukov at 29-Jan-13 6:13am
   
Thank you, Carlo.
I don't know why would you dislike 'archaic'. I learned nearly modern separate compilation technologies when C++ was far from its modern form, without exceptions and many other features. Even at that time it looked archaic...
—SA
steven8Gerrard at 29-Jan-13 7:25am
   
Thanks for the reply. Much appreciated
Sergey Alexandrovich Kryukov at 29-Jan-13 12:46pm
   
You are very welcome.
Good luck, call again.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

A C/C++ program consists of 'units'. These units are the source files, .c in C or .cpp in C++. Before the compilation starts a process called translation takes place: first, all the #include directives are resolved, which basically means the content of the included header files is copied in the .c/.cpp file, in the place of the #include directive. Then, resolving of macros takes place, and the resulting .c/.cpp file is called a 'translation unit'.
 
Now, the problem with declaring variables in a header that is including in multiple source files, is that it generates declarations of variables with same name in multiple translation units. The linker doesn't like that, because all symbols must be uniquely identifiable. Therefore you have two choices, declare them in the source file directly, or use the extern keyword, as already instructed.
  Permalink  
Comments
CPallini at 29-Jan-13 4:55am
   
5.
steven8Gerrard at 29-Jan-13 7:25am
   
Thanks for the reply. Much appreciated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 495
1 Maciej Los 258
2 OriginalGriff 229
3 BillWoodruff 170
4 Zoltán Zörgő 165
0 OriginalGriff 7,934
1 DamithSL 6,084
2 Sergey Alexandrovich Kryukov 5,449
3 Maciej Los 5,174
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 29 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100