In this tutorial, we will study the concept of header files in C so that later we can create modular programs.
What is Header file?
How to use it?
Let's learn.
What is a File Header?
Header files are files with extensions .h
that contain definitions of functions, variables, macros, and constants.
In the programs that we have created, we often insert header files stdio.h
into the program.
#include <stdio.h>
File stdio.h
is a header file that contains definitions of functions for standard input and output such as printf()
, scanf()
, gets()
, puts()
, and so on.
stdio.h
Where are the files ?
If you use Linux, the files stdio.h
will be in the /usr/include/
.
If you use Windows, the file stdio.h
will be in the compiler folder, for example C:\MinGw\include
.
We can open the file stdio.h
with a text editor and see its contents.
Why do we Need File Headers?
The file header in C actually acts as a library.
What is a library?
A library in programming is a collection of functions and constants that we can reuse.
For example:
We are making a large scale program. This program has thousands of codes. So we can't possibly write the program in just one file.
To make it easy to manage, programs must be made into modules or libraries by writing them in separate files.
That way, we only need to import and use it in the main program.
So why do we need file headers?
Because you can make programs modularly so that the program is easy to manage and develop.
How to Import File Headers
There are two ways to import file headers into the program.
First way: Using corner brackets
#inlude <stdio.h>
This is used to import header files located in the system folder (/usr/include)
.
Second way : Using quotation marks
#include "nama_file.h"
The second method is used to import header files that are still in the same folder as our program.
To understand better, let's try practicing.
Practice: Example Program with File Headers
Create a new folder with the name header_file
, then in it create three files:
main.c
Main program files ;hitung_umur.h
Age count file header file;hitung_umur.c
Implementation code file of the header file.
Fill each file with code like this:
Files:hitung_umur.h
int hitung_umur(int tahun_lahir, int tahun_sekarang);
Files:hitung_umur.c
int hitung_umur(int tahun_lahir, int tahun_sekarang) {
return tahun_sekarang - tahun_lahir;
}
Files:main.c
#include <stdio.h>
#include "hitung_umur.h"
void main() {
int tahun_sekarang = 2022;
int tahun_lahir = 1999;
// menggunakan fungsi dari hitung_umur.h
int umur = hitung_umur(tahun_lahir, tahun_sekarang);
printf("Umur kamu %d tahun", umur);
}
After that, we can compile all the programs.
Because here there are two c file code files, we have to compile both files.
If you just compile it main.c
, it won't work.
So how do you compile these two files?
All you have to do is add all the file names you want to compile to the command gcc
.
Example:
gcc main.c hitung_umur.c -o main
So the result is:
The program has been successfully compiled, to run it type the following command:
./main
So the result is:
Practice: Using Codeblocks
Open Codeblocks, then click the File menu -> New Project.. . Then select Console application
After that click Next .
Select C as the programming language to be used. After that click Next .
After that, fill in the project name with latihan_header_file
, then click Next .
In the compiler selection, select GNU GCC Compiler . After that click Finish .
Our project is ready...
Let's add a new file.
The method:
Click the File menu -> New File , then select Empty File .
If a window like this appears:
Click Yes .
Then fill in the file name hitung_umur.h
and click Save .
If it appears like this, just click OK .
After that, fill the file hitung_umur.h
with code like this:
int hitung_umur(int tahun_lahir, int tahun_sekarang);
Then, create one more file to implement hitung_umur.h
, namely hitung_umur.c
.
The method is the same as before.
Click File –> New File –> Empty File . Then give it a name hitung_umur.c
and fill in the code like this:
int hitung_umur(int tahun_lahir, int tahun_sekarang) {
return tahun_sekarang - tahun_lahir;
}
Finally, change the code to main.c
look like this:
#include <stdio.h>
#include <stdlib.h>
#include "hitung_umur.h"
int main()
{
int tahun_sekarang = 2022;
int tahun_lahir = 1999;
// menggunakan fungsi dari hitung_umur.h
int umur = hitung_umur(tahun_lahir, tahun_sekarang);
printf("Umur kamu %d tahun", umur);
return 0;
}
After that try Compile and Run .
So the result is:
Great 👍
What is next?
Header files help us make programs modular, so we don't have to write code over and over again.
Just import the header file from another program and it can be used straight away.
Next, try making your project using this technique and also try to learn about make files in C.
Post a Comment