Salute to all C programmers and wizards out there,
I have a question about the EDU-RC default code. Ifi_utilities.c seems to contain “useful” functions which we can call in our code. This behavior seems similar to that of a library or header file. If I am right in thinking so, why isn’t it a header file? Or if I am wrong, could somebody please inform me as to where I am wrong.
It’s really just the way most C programs are structured. Since it’s a library, it’s a .C file. Header files are usually used for prototyping functions, declaring GLOBAL variables and constants, and including/requiring the needed source files.
It’s not mandatory that the code be in a .C file (I think…), but it’s just the standard way to do things…
*Originally posted by Venkatesh *
Ifi_utilities.c seems to contain “useful” functions which we can call in our code. This behavior seems similar to that of a library or header file.
This is correct - it is a library.
If I am right in thinking so, why isn’t it a header file?
There is also a header file (ifi_utilities.h) included in the distribution. A header file is used by other source (.c) files to know what the function names are and what the arguments are. This is because the header files contain function declarations:
void PrintByte(unsigned char odata);
void PrintWord(unsigned int odata);
However, the declarations only tell the C compiler what the functions are, not what they do. That is what the function definition is for:
void PrintByte(unsigned char odata)
Hex_output((unsigned char) odata);
TXREG = 13; /* a carriage return */
Function definitions belong in C files, function declarations belong in header (.h) files. It is possible to put code into header files, however there are many problems with this and it is widely considered to be a very bad thing to do.
Sorry if I oversimplified it, but maybe this will help other people understand the difference a little better too…