Learn C #19: Understanding File Headers

 


Understanding File Headers in C

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 .hthat contain definitions of functions, variables, macros, and constants.

In the programs that we have created, we often insert header files stdio.hinto the program.

#include <stdio.h>

File stdio.his a header file that contains definitions of functions for standard input and output such as printf()scanf()gets()puts(), and so on.

stdio.hWhere are the files ?

If you use Linux, the files stdio.hwill be in the /usr/include/.

stdio files

If you use Windows, the file stdio.hwill be in the compiler folder, for example C:\MinGw\include.

folder include mingw

We can open the file stdio.hwith 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.

file header on c

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:

  1. main.cMain program files ;
  2. hitung_umur.hAge count file header file;
  3. hitung_umur.cImplementation code file of the header file.

program file 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.

compile main

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:

results of the compiled program

The program has been successfully compiled, to run it type the following command:

./main

So the result is:

program output results

Practice: Using Codeblocks

Open Codeblocks, then click the File menu -> New Project.. . Then select Console application

codeblock-new-project

After that click Next .

welcome wizard

Select C as the programming language to be used. After that click Next .

select language

After that, fill in the project name with latihan_header_file, then click Next .

project-name

In the compiler selection, select GNU GCC Compiler . After that click Finish .

compiler stump

Our project is ready...

projects in codeblocks

Let's add a new file.

The method:

Click the File menu -> New File , then select Empty File .

If a window like this appears:

add files to project

Click Yes .

Then fill in the file name hitung_umur.hand click Save .

file-name

If it appears like this, just click OK .

select target file

After that, fill the file hitung_umur.hwith 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.cand 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.clook 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:

codeblocks run results

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

Previous Post Next Post