如何在 C 中編寫自己的標頭檔案?\n
我們將瞭解如何使用 C 建立自己的標頭檔案。要建立一個頭檔案,我們必須建立一個名稱和副檔名為 (*.h) 的檔案。該函式中將沒有 main() 函式。在該檔案中, możemy 某些變數、某些函式等。
要使用該標頭檔案,它應位於程式所在的同一目錄中。現在,使用 `#include`,我們必須放入標頭檔案名。名稱將被包含在雙引號中。`include` 語法將如下所示。
#include”header_file.h”
讓我們來看一個程式來獲得這個概念。
示例
int MY_VAR = 10; int add(int x, int y){ return x + y; } int sub(int x, int y){ return x - y; } int mul(int x, int y){ return x * y; } int negate(int x){ return -x; }
示例
#include <stdio.h> #include "my_header.h" main(void) { printf("The value of My_VAR: %d\n", MY_VAR); printf("The value of (50 + 84): %d\n", add(50, 84)); printf("The value of (65 - 23): %d\n", sub(65, 23)); printf("The value of (3 * 15): %d\n", mul(3, 15)); printf("The negative of 15: %d\n", negate(15)); }
輸出
The value of My_VAR: 10 The value of (50 + 84): 134 The value of (65 - 23): 42 The value of (3 * 15): 45 The negative of 15: -15
廣告