473,386 Members | 1,694 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,386 software developers and data experts.

what is header file and how it works in c/c++ programming?

what is header file and how it works in c/c++ programming?
Aug 13 '10 #1
5 3912
Meetee
931 Expert Mod 512MB
Header files contain declarations so that compiler understands what to do when it finds certain functions in the code.
For eg. when cout is there in some code, it never defines cout, so the compiler would know what cout is by seeing header “iostream” which we usually define as
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
Aug 13 '10 #2
donbock
2,426 Expert 2GB
What we casually call compilation actually consists of three steps: preprocessing, compiling, linking.

The preprocessor opcodes all start with "#" (#include, #define, #if, etc.). In particular, it is the preprocessor that knits together all the files referenced by #include's into a single virtual source file that is passed to the subsequent build steps. The preprocessor neither understands nor cares what our intended purpose is for all those #included files; and the fact that certain pieces of the virtual source file came from different #included files is hidden from the compiler and linker.

Over the years, the C/C++ programming community have developed idioms and style rules for how best to use the #include feature. The term "header file" comes from the style rule that all #include statements ought to be at the top of the source file.

To understand header files and how they work you need to first understand the underlying #include feature and then the idioms and style rules that govern its use.
Aug 13 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
Adding to what has been said, suppose you have a struct of your own. Let's say it has 100 members. And let's say you have 1000 source files in your program.

Your choices are:
1) maintain a copy of the struct in each of the 1000 files. So when you add/remove a member of the struct you will have to change 1000 files.
2) put the struct in a header file and #include the header file in the 1000 source files. Now when you add/remove a struct member you change only the struct in header file and recompile your program.

Just pick choice 1 or choice 2.
Aug 13 '10 #4
Header file contains functions, for example string.h header file contains function related to string like strrev, strcat etc. if you want to use that function then you have to add that header file. You can read more about header file here, It helped me a lot.
Feb 6 '12 #5
Actually, string.h doesn't contain the functions themselves, it only contains the prototypes of those functions. The functions themselves reside in the library, and are linked to the program by the linker. But because those functions are being called before they are linked in, the compiler needs to know about them and the header files provide the basic information about them.

You can put anything in a header file that you want - it just gets typed out into your program by the pre-processor. For example, I could make a file called "myheader.h" and put this in it: Hello, my name is coderFoo! Then, I could put this line in my program:

Expand|Select|Wrap|Line Numbers
  1. #include<myheader.h>
and then that header file would be replaced by the phrase: Hello, my name is coderFoo! and of course it would cause an error from the compiler.
In other words, whatever is in the header file is simply typed out where it's at when you start to compile the program.

You can put function definitions in there if you want, but all of the standard library functions remain in the library until compile - link time, and only the function prototypes are in those header files.
Feb 7 '12 #6

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

Similar topics

4
by: Newsgroup - Ann | last post by:
I have a library function like GetOptions( ..., struct net_arch_t netArch, ....) and put the declaration into a regular header file like getopt.h. But this function declaration also needs the...
5
by: G?nter Omer | last post by:
Hi there! I'm just trying to compile a header file (unsing Borland C++ Builder 10)implementing a class, containing the declaration of the STL <list> but it refuses to work. The following...
3
by: JKop | last post by:
I'm writing a ".cpp" and ".hpp" combination. The ".hpp" just contains one sole function declaration. The ".cpp" makes use of the function "std::strlen" and the type "std::size_t".
15
by: Kannan Goundan | last post by:
Maintaining a C++ header file can be painful. I want a way to automatically generate header files from the implementation file. Does anybody know of a program that does this? If not, I'd like...
12
by: blueblueblue2005 | last post by:
Hi, here is an example I copied from Deitel C++ book. but when I compile it, always get the above compilation error, no matter how I change the include order, please help. here is the files:...
5
by: Jim Holder | last post by:
I tried building libpcap and tcpdump from my Red Hat 7 RPMs. The tcpdump make couldn't find ioccom.h and sockio.h. When a header file is missing, how do you find it? Thanks.
9
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
5
by: LKavitha4 | last post by:
hi, Please tell me what header file is used for sleep ( ) function in linux c++. Thank you.
0
by: abarun22 | last post by:
Hi I am facing a problem while including a C header file in the SWIG interface file. However the problem does not occur when i directly copy the contents of header file in the same place. My...
6
by: bobby | last post by:
hi group, Does the header file size or number in include(s) effect the size of executable file? In other world if i chose a large header file and include it with my source file does it increase...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.