Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
I am splitting up a program into multiple source files for the first time ...
I'm splitting up a program into multiple source files for the first time, and I'm wondering what to put in .c files and what to put in .h files. (What does ``.h'' mean, anyway?)
✍: Guest
As a general rule, you should put these things in header (.h) files:
macro definitions (preprocessor #defines)
structure, union, and enumeration declarations
typedef declarations
external function declarations
global variable declarations
It's especially important to put a declaration or definition in a header file when it will be shared between several other files. Don't repeat a declaration or macro definition at the top of two or more source files; do put it in a header file and #include it wherever needed. The reason is not just to save typing: you want to set things up so that whenever a declaration or definition changes, you only have to change it in one place, with the update propagated to all source files consistently. (In particular, never put external function prototypes in .c files.
On the other hand, when a definition or declaration should remain private to one .c file, it's fine to leave it there. (Private file-scope functions and variables should also be declared static.
Finally, you should not put actual code (i.e. function bodies) or global variable definitions (that is, defining or initializing instances) in header files. Also, when you are building a project out of multiple source files, you should compile each of them separately (using a compiler option to request compilation only), and use the linker to link the resultant object files together. (In an integrated development environment, all of this may be taken care of for you.) Don't try to ``link'' all of your source files together with #include; the #include directive should be used to pull in header files, not other .c files.
2016-02-18, 1060👍, 0💬
Popular Posts:
What print out will the folloging code produce? main() { char *p1=“name”; char *p2; p2=(char*)malloc...
What is application domain? Explain. An application domain is the CLR equivalent of an operation sys...
1. What is normalization. 2. Difference between procedure and functions. 3. Oracle 9i Vs 10g. 4. how...
How can I search for data in a linked list? Unfortunately, the only way to search a linked list is w...
What will be printed as the result of the operation below: main() { int a=0; if (a==0) printf("Cisco...