h files usage in C?

Status
Not open for further replies.

legolas

Padawan
hi,

suppose i have a C file in which i include a "header file" i created. then, can i again modify this C file into a header file which in turn calls another header??? i knew that "header files" contains definitiions alone.. but u must also know that even if we type in the function in the h file and save it as such, it cud be called instead of defining alone and writing remaining in another C file. so, with this, the situation is

at first i write a program sample.c as...

#include <...>
....
#include "sample1.h"

void main()
{
.......
}

now i modify this sample.c to sample.h, with no main, and only function declarations similar to how i did the sample1, with sample1 stil being used in the new sample.h file...

if i create a sample2.c and use sample.h, does it mean that sample1.h is also included? i guess no... but how do i do it ?? wil adding sample1.h itslef fix the problem?? guess its confusing too! :( sorry for that.. incase.

/legolas
 

popper1987

Broken In
you are just creating links ,so the answer is sample1.h is also included,if you want to check just remove the line "#include "sample1.h" " from sample.h and it will give you an error of undefined variables
 

tuxfan

Technomancer
I think both the files will be included. You are trying for inheritance of header files :p

I have tried this earlier in Turbo C. I used to include a lot of header files that I made. Typing them everytime was a pain, so I had a inclall.h and I used to include just that and it used to work :)
 

sujithtom

Ambassador of Buzz
It plain words yes.
Some new C packages which tries to obey the ANSI standard (Header files should not have .h extension) simply creates a file with the header file which has a link to the orginal header file
For eg
Orginal header file stdio.h
Header file user includes stdio
Contents of stdio :

#include "stdio.h"
 

anubhav_har

In the zone
Here is the description of header files or .h files::
A header file is a file containing C declarations and macro definitions (see Macros) to be shared between several source files. You request the use of a header file in your program by including it, with the C preprocessing directive #include.

Header files serve two purposes.

* System header files declare the interfaces to parts of the operating system. You include them in your program to supply the definitions and declarations you need to invoke system calls and libraries.
* Your own header files contain declarations for interfaces between the source files of your program. Each time you have a group of related declarations and macro definitions all or most of which are needed in several different source files, it is a good idea to create a header file for them.

Including a header file produces the same results as copying the header file into each source file that needs it. Such copying would be time-consuming and error-prone. With a header file, the related declarations appear in only one place. If they need to be changed, they can be changed in one place, and programs that include the header file will automatically use the new version when next recompiled. The header file eliminates the labor of finding and changing all the copies as well as the risk that a failure to find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with .h. It is most portable to use only letters, digits, dashes, and underscores in header file names, and at most one dot.
 
Status
Not open for further replies.
Top Bottom