Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Updated 28-Jan-13 22:28pm
v3

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.
 
Share this answer
 
Comments
Argonia 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 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 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 29-Jan-13 4:17am    
Yes. Source, not header.
Argonia 29-Jan-13 4:21am    
Thank you
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
 
Share this answer
 
Comments
Joezer BH 29-Jan-13 4:26am    
5+
Sergey Alexandrovich Kryukov 29-Jan-13 6:13am    
Thank you, Edo.
—SA
CPallini 29-Jan-13 4:55am    
5, in spite of 'archaic C++ technology' :-)
Sergey Alexandrovich Kryukov 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 29-Jan-13 7:25am    
Thanks for the reply. Much appreciated
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.
 
Share this answer
 
Comments
CPallini 29-Jan-13 4:55am    
5.
steven8Gerrard 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900