Starting a Project in C / Manage files in C project (Creating own header files in C)
Jan 1, 2018Most of the student's in C progamming wants to do a project for thier course objective as well as for their own. But the complexity arises when the source code happens to grow larger. They can find themselves uneasy while writing a program source code. So, this topic will be helpful to those students who wants to write manageable codes in C programming or simply who wants to create their own library in C.
C comes with useful library headers like stdio.h header. Here we will create our own header for example i am creating library file for circle (circle.h). I will show both using code::blocks ide as well as using text editor.
Using Code::Blocks IDE
Firstly open the code::blocks installed in your system. I am opening the code::blocks in my ubuntu 16.04. Briefly we are going to do.
I) Creating main C (main.c) file that that contains our projects main function.
II) Creating header file (circle.h) and its c file(circle.c). and use this file as library in my main program.
What is header file in C ?
It is a file with extension .h . Header file contains C function declarations and and macro definations to be shared between several source files. The header file stdio.h is header files that comes with C compilers or during the installation of IDE like code::blocks. A header library can be used in a C program by C preprocessing directive #include . In practice header file keep all the constants, macros, system wide global variables, and function prototypes in the header files and include that header file wherever we want to use.
The header file can be included in two ways with following syntax.
1) #include
Here this type of inclusion is used for system library files. It searches library file called lib_file in the standard list of system directories.
2) #include "lib_file"
This type of inclusion searches for the header files of your own in the directory containing the current file. A relative path can be given for the library file(for example if the lib_file.h is located inside the folder called my_folder the it must be included like #include "my_folder/lib_file.h")
Creating own header called circle.h
#ifndef circle
#define circle
/*defining the macro for PI*/
#define PI 3.1416
/*function declarations*/
float circleArea(float radius);
float circleCircumference(float radius);
#endif
As you are wondering why the hell that extraunfamiliar code is for the reason is, If we include the header file twice then compiler will include the file twice and results error due to duplication of code. So for prevention of this type of mistakes we use construct #ifndef to prevent including the file twice. Its syntax is
#ifndef HEADER_FILE
#define HEADER_FILE
the entire header file file
#endif
Creating source file circle.c
/*including its header file*/ #include "circle.h" /*function definations*/ float circleArea(float radius){ return PI * radius * radius; } float circleCircumference(float radius){ return 2 * PI * radius; }
Now finally main file main.c
#include <stdio.h> /*including the header file we created*/ #include "circle.h" void main(){ float radius,cArea, cCircumference; /*reading circle radius*/ printf("Enter the radius:"); scanf("%f",&radius); /*using the function from circle.h*/ cArea = circleArea(radius); cCircumference = circleCircumference(radius); /*printing the computed value*/ printf("\n Area=%f \n Circumference=%f\n", cArea,cCircumference); }