Have you just started programming and saw the line #include <stdio.h>? Are you wondering what this so called “.h” file is and what it does and why it’s important? Well in the following article I’ll go into detail about what a header file is, what its purpose is and also the most common header files.
A header file is any file that ends with the .h extension and contains C declarations and macro definitions that are going to be shared between several source files. A header file’s most common use is to store functions and declarations that are used often and in different source files. You don’t want to continuously write out the same function declaration 50 times. But how do you use header files? How do you create your own? Those are great questions and I’ll answer both.
How do I use header files?
To use a header file you need to include it. You can include it two different ways depending on whether or not the header file is a system header file (one that’s included with the language) or one you have made. If the header file is a system header file then you can include it by inserting this line at the top of your program #include <name-of-header-file.h>. If the file is one that you created and it’s located in the same directory as your source code then you put this line at the top of your program #include "name-of-header-file.h" (notice the quotation marks instead of the < > symbols).
How do I create my own header files?
It’s incredibly easy to create your own header files. Put all of your function declarations and macro definitions into a file and save that file with a .h extension (not a .c extension). Once you’ve created the file all thats left to do is include it and you can do that by using this line at the top of your program #include "name-of-header-file.h"
So you now know how to create your own header file and include it into your program, but how do you know which system header files to use and whats included in them? If you’re on a unix system you can use the man pages to find the header file that you need (just man the function you want and it’ll say which header to use). Here is a great list of functions along with the needed header files.
Below is a list of important header files and the functions that they contain
stdio.h
EOF, NULL, stdin, stdout, stderr
int printf(const char *, ...);int scanf(const char *, ...);int getc(FILE *);FILE fopen(const char *, const char *);int feof(FILE *);- lots more
stdlib.h
EXIT_SUCCESS, EXIT_FAILUREint abs(int);int atoi(const char *);void malloc(size_t);void free(void *);void qsort(void *, size_t, size_t, int (*)(const void *, const void *));- lots more
math.h
M_E (value of e), M_PI (value of
)double exp(double);double floor(double);double sqrt(double);double remainder(double, double);- lots more
Hopefully this short tutorial on header files has helped you get a better grasp on the C programming language. Go and program!
Entries (RSS)
September 4th, 2007 at 10:14 pm
Great tutorial, I’m new to C++ and I was wondering what exactly their purpose was.
June 3rd, 2008 at 8:31 am
Like you said,it is incredibly easy to create one’s own header file. Had I known it earlier,I would have definitely landed that job!
August 5th, 2008 at 12:11 pm
thx man. header = storage of functions.