473,406 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Header file help

Say I wanted to create a header file for my program. Would the variables in the header be defined globally for the file that it is attached to.
Expand|Select|Wrap|Line Numbers
  1.  
  2. //header.h
  3.  
  4. //#ifndef HEADER_EXAMPLE 
  5. //#define HEADER_EXAMPLE
  6. char array[10] ={"hello world"};
  7.  
  8. //prog.c
  9. //#endif
  10.  
  11. #include <stdio.h>
  12. #include "header.c" //#include <header.c>
  13. {
  14. printf("%s", &array);
  15.  return 0;
  16. }
  17.  
don't really understand the parts that I commented out. What does the header_example mean. Is it supposed to be a specific word of anything or its just random words?
Nov 1 '07 #1
3 1482
weaknessforcats
9,208 Expert Mod 8TB
Say I wanted to create a header file for my program. Would the variables in the header be defined globally for the file that it is attached to.

Code: ( text )
//header.h

//#ifndef HEADER_EXAMPLE
//#define HEADER_EXAMPLE
char array[10] ={"hello world"};

//prog.c
//#endif

#include <stdio.h>
#include "header.c" //#include <header.c>
{
printf("%s", &array);
return 0;
}
Let's takes this in pieces:

1) This header can be included in many source files. Each time it is included, its contents become part of the object file for that source file. When the linker combines the object files to create your executable, and variables created in the header file will be duplicated in each object file. Your build dies here with redefinition errors.

RULE 1: Header files contain declarations and not definitions.

A declaration says a thing exists. A definition is the code that creates it.

Expand|Select|Wrap|Line Numbers
  1. extern int data;   //declaration. data is not in this file
  2.  
  3. int data;             //definition. data is created right here
  4.  
  5. void MyFunction(int);  //declaration. The function is not in this file
  6.  
  7. void MyFunction(int arg)  //definition. This is the actual function.
  8. {
  9.     cout << arg << endl;
  10. }
  11.  
The business with the #ifndef has to do with protecting against complications resulting from including the same header more than once in the same source file.

You get into this pickle when you include HeaderA and HeaderB and each of these includes HeaderC. When Header C is included the second time you die with redefinition errors again.

So, you need to be sure HeaderC is include only once. The common practice is to use an inclusion guard. This is a series of preprocessor directives.

Expand|Select|Wrap|Line Numbers
  1. #ifndef XXX
  2. #define XXX
  3.  
  4. // header file contents here
  5. #endif
  6.  
The first time the header is included XXX is not define. Hence the "if-not-defined" preprocessor directive evaluates to true and you define XXX and then include the header file contents. The #endif terminates the area affected by the #ifndef.

The second time the header is included (in the same source file, remember) the #ifndef evaluates to false because XXX was defined during the first inclusion. Hence, the preprocessor skips to the #endif neatly bypassing the header file contents.

RULE 2: All header files need inclusion guards.

RULE 3: The symbol defined (XXX) needs to be unique among all header files ever written or this scheme doesn't work. Making XXX a unique thing is left to your imagination.

RULE 4: Because of RULE 1, you never include source files.
Expand|Select|Wrap|Line Numbers
  1. #include <MyStuff.c>        //Big no-no.
  2. #include <MyStuff.cpp>
  3.  
Nov 1 '07 #2
GGnOrE
8
1.)Thanks for the advice weaknessforcates

2.)
Okay,
When should I use multiple header files. It doesn't seem necessary if that one header file may be include at the top of every file. Also, functions can't access the declarations made by the header, so I pretty much have to make local variables for those.
Nov 1 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
When should I use multiple header files. It doesn't seem necessary if that one header file may be include at the top of every file. Also, functions can't access the declarations made by the header, so I pretty much have to make local variables for those.
A header file should contain related declarations. Unrelated declarations shoul bd in separate header files. For example, you have Date and a Person. It would make little sense to carry Person declarations into an application that only needs a Date. Here you should have separate header files.

Nothing prevents the Person header file from including the Date header file so that the Person user only has to include the Person header.

Header file management is a learned skill.

Functions can access declarations in the header. I don't see the connection between that and having to create local variables.

Do you have a example of what you are talking about?
Nov 2 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Thomas Kemmerich | last post by:
hi, i'm looking for a possibility to start a download. here is my current code: header("Content-Type: $_contenttype"); header("Content-Disposition: attachment; filename=\"$_filename\"");...
11
by: DaRemedy | last post by:
Hiya, just need help with PHP headers. I have an index php page which has the following code within a header redirect: <?php if ( empty($_GET) ) if (empty($_GET) ) { $month = date(n);
0
by: Luke Airig | last post by:
I am using the Saxon engine and I have an xml file that contains batches of records. Each batch starts with a header record and the associated detail records immediately follow each header. There...
16
by: matthurne | last post by:
I just started learning C++ on my own...I'm using Accelerated C++. Something it hasn't explained and I keep wondering about is how header files actually work. I suspect it doesn't get into it...
16
by: Michael | last post by:
I have a data application in a2k that I need to create two fixed width text files and then combine them to a single file The first file is header information and the second is transaction data. ...
6
by: alan | last post by:
Dear all, I have written my own function by C. And my development platform is W2k with VC6.0. Then I also defined a header file to extern declare this function. After that, I include this...
3
by: bill | last post by:
I firmly believe that it is always a bad idea to put code in a header file. Nothing pisses me off more than seeing function definitions in a ..h, and I recently was truly blessed :) to witness...
16
by: wdh3rd | last post by:
Hi everyone. I'm new to C and I have a few questions: I am making files for permutations and combinations. Files to be made are perm.c, perm.h, combo.c, and combo.h. Since both combinations...
1
by: Shalako | last post by:
I check my error log and see these entries: malformed header from script. Bad header= Missing gauge reports are ind: padata.pl /perl/pema/padata.pl did not send an HTTP header malformed...
1
by: Proogeren | last post by:
I have a problem with a httpwebrequest that I am creating. The request in itself looks correct but using fiddler I see that a www-authentication header is sent along as well. The code is pasted...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.