What are C Source Files?
A C application is primarily compiled and built from source and header files (.c and .h files). A source file normally contains application’s logic and implementation that performs certain tasks. A header file normally contains declaration and prototypes of your logic / functions implemented in the source file. A function prototype is important as it tells the compiler what input and output to expect from a function implementation.
This is an example of a source file (myfuncs.c
) that implements 2 functions:
- printfHelloWorld()
- promptAndPrint()
/* myfuncs.c */ #include <stdio.h> void printHelloWorld(void) { printf("Hello World\n"); } void promptAndPrint(void) { int iNumber = 0; printf("Enter an integer please: "); scanf("%d", &iNumber); printf("You entered: %d", iNumber); }
What are C Header Files?
This is an example of a header file (myfuncs.h
) containing the function prototypes implemented in myfuncs.c. Other source files can gain access to these 2 functions by #include
this header file.
/* myfuncs.h */ void printHelloWorld(void); void promptAndPrint(void);
How to Include Other Header Files to Gain Access?
Another source file (myprogram.c
) that contains the entry point (also known as the main()
function) can gain access to both printHelloWorld()
and promptAndPrint()
functions simply by including the respective header file using the #include
preprocessor clause. More on preprocessor here.
/* myprogram.c */ /* include standard C IO library */ #include <stdio.h> /* include user-defined myfuncs.h */ #include "myfuncs.h" int main(void) { /* call the functions defined in myfuncs.c */ printHelloWorld(); promptAndPrint(); return 0; }
Write Comments in C Source and Header Files?
Source and header files do not have to include only source codes. You can also write comments, stories or paragraphs that describe what your code is about and will not be compiled by the compiler. A comment can start with double forward slashes (//
) or enclose with /*
and */
.
/* * myfuncs.c: * This is a comment block */ #include <stdio.h> #include <stdlib.h> void printHelloWorld(void) { // this is a line of comment printf("Hello World\n"); /* this is also a line of comment */ printf("Hello World Again!\n"); }
Do Not Forget the Semicolons
A semi colon is a special character that indicates the end of line. You must include it in every single line of source (except for preprocessor declarations). If you forget about it, you will get a compilation error.
/* * myfuncs.c */ #include <stdio.h> #include <stdlib.h> void promptAndPrint(void) { /* every line must end with semicolon ; */ int iNumber = 0; printf("Enter an integer please: "); /* you can put codes in one line as long as they are separated by semicolon ; */ scanf("%d", &iNumber); printf("You entered: %d", iNumber); printf("end of function"); }
Related Posts
- Setting up Eclipse IDE for C on Ubuntu Linux Made Simple
- How does a Compiler Work? That is the Question.
Hi, this is Cary, your friendly tech enthusiast, educator and author. Currently working as a software architect at Highgo Software Canada. I enjoy simplifying complex concepts, diving into coding challenges, unraveling the mysteries of software. Most importantly, I like sharing and teaching others about all things tech. Find more blogs from me at highgo.ca