ขั้นตอนการเขียน header file เองใน C:
- เขียนโค้ดแล้วเซฟเป็น "sub.h"
- เขียนโปรแกรมหลัก "subtraction.c" ซึ่ง:
- include header file ใหม่
- เขียน "sub.h" แทน <sub.h>
- ฟังก์ชันใน sub.h พร้อมใช้งาน
- เรียกฟังก์ชัน sub() โดยตรง
- "subtraction.c" และ "sub.h" ต้องอยู่โฟลเดอร์เดียวกัน
Sub.h
int sub(int m, int n) { return(m-n); }subtraction.c
ตัวอย่าง
#include<stdio.h> #include "sub.h" void main() { int a = 7, b = 6, res; res = sub(a, b); printf("Subtraction of two numbers is: %d", res); }หลังรัน "subtraction.c" ผลลัพธ์จะเป็น:
ผลลัพธ์
Subtraction of two numbers is: 1