-
.h file vs .c file
Hi all,
Just a quick question. My one .c file programs are starting to get very large. To break this down a bit, I can either create more .c files in the same project or put some code in .h files. I take it, if the code is going to be changed, put it in .c files, but if code is constant and never changed, put in a .h file. Is this correct?
Look forward to your reply.
RocketMan
-
No. ".h" is a typical file extension for C header files. ".c" is a typical file extension for C source files.
Therefore, put your implementation code in the source files, and also provide the appropriate header files with the function, struct and other declarations that the source files will need.
-
Hi,
Sorry I will re-word that. As an example if I have functions - 1ms, 10ms, 1sec, 1min etc. Do I make a header file delay.h or make another "c" file delay.c? A quick example is below:
Code:
#include<stdio.h>
#include<delay.h>
int main()
{
while(1)
{
printf("Hello\n");
delay_1ms(5); // this function should be in header file ? ? ?
}
Cheers
RocketMan
-
Both. The header might be something like this:
Code:
#ifndef DELAY_H_
#define DELAY_H_
/* delay.h */
void delay_1ms(int amount);
void delay_10ms(int amount);
void delay_1sec(int amount);
void delay_1min(int amount);
#endif
and the source file might be something like this:
Code:
/* delay.c */
#include "delay.h" /* May not be necessary in this case. */
void delay_1ms(int amount) {}
void delay_10ms(int amount) {}
void delay_1sec(int amount) {}
void delay_1min(int amount) {}
Of course, I would rather have one delay function that delays in multiples of a small quantum instead of multiple delay functions that delay in multiples of different quanta.
-
Hi,
Ok thanks, I will give it a go.
Cheers
RocketMan
-
I only use header files for all the common stuff between the c files, I never put actual code in it. Just prototypes, #defines, and typedefs.