What's the difference between a .c file and a .h file? I though .h was just for modules, but I hear people calling it header files. Also, everyone seems to have .h .c files for each module.
What's the deal?
Thanks!
What's the difference between a .c file and a .h file? I though .h was just for modules, but I hear people calling it header files. Also, everyone seems to have .h .c files for each module.
What's the deal?
Thanks!
.h files are called header files, they should not contain any code (unless it happens to contain information about a C++ templated object). They typically contain function prototypes, typedefs, #define statements that are used by the source files that include them. .c files are the source files. They typically contain the source code implementation of the functions that were prototyped in the appropriate header file.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
So, is it good practise to prototype your functions in header files? I've gotten away with it enough.
a.h:
void function();
a.c
void function(int *parameter)
{
}
> void function();
This isn't a prototype, it's an old-style function declaration.
This is a prototype (for the example you list)
void function(int *);
Or even better
void function(int *parameter);
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Is that still a valid declaration? That's AT&T style, isn't it? Very scary, if you've seen a lot of 80s code.Originally Posted by Salem
I've always kept my function bodies in .h files and included them as modules in my main.c file. I understand this is bad practise. Do I declare the function prototype in the header file, and then use a .c file with the same name as the header, and include the header in main.c to include my functions?
> Do I declare the function prototype in the header file, and then use a .c file with the same name as the header
Yes, that's exactly it.
Then you compile both source files together and the linker works its magic to combine both object files into a runnable program.
> Is that still a valid declaration?
As far as I know.
Though I find omitting the variable name odd since it's extra work over simply copy/pasting the function definition, and you lose useful documentation in the process.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I always use the header declarations to find the functions I need to use that are in the c file. So taking away the variable names from a function definition is bad for me :(Originally Posted by Salem
The only requirement of a function prototype is that it have a return value, function name, and an optional parameter type list. Variable names are optional.Originally Posted by SMurf
In fact, variable names need not even be the same. The following is legal:Originally Posted by Kleid-0
This will compile without warnings using "-Wall -ansi -pedantic".Code:#include <stdio.h> void foo( int bar ); int main( void ) { foo( 4 ); return 0; } void foo( int baz ) { printf("baz is %d\n", baz ); }
Variable names are purely optional, and as long as the types and order are the same in the actual definition, they can be called anything you want, provided it's a valid potential variable name.
Quzah.
Hope is the first step on the road to disappointment.
What I often do is what quzah described above. I'll use a verbose parameter name in the header file so that, combined with comments, it becomes clear what the parameter is for. In the actual source file I'll use a normal meaningful variable name.