如何在 C 中編寫自己的標頭檔案?


在 C 中編寫自己的標頭檔案的步驟 −

  • 鍵入程式碼並將其另存為“sub.h”。
  • 編寫一個主程式“subtraction.c”,其中 −
    • 包含新的標頭檔案。
    • 寫入“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

更新日期:2019 年 7 月 30 日

1 千+ 次瀏覽

開啟您的 職業

完成課程即可獲得認證

開始
廣告
© . All rights reserved.