1. 程式人生 > >C語言入門(六)之include、多檔案開發

C語言入門(六)之include、多檔案開發

include基本概念

#include <stdio.h> // 告訴系統printf函式是存在的, 告訴系統printf函式的格式(宣告printf函式)

// include的作用, 是將include右邊的檔案拷貝到當前檔案中

int main(int argc, const char * argv[]) {
    //     include指令不一定要寫在檔案的頭部
//#include "abc/123.txt"
#include "/Users/123.txt"
    
    return 0;
}
/*
 include後面的 <> 和 "" 的區別
 >如果使用<>代表會先從開發工具的編譯環境中去查詢
    + /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/
 
 >如果編譯環境中沒有找到, 那麼會再去系統的編譯環境中找
    + /usr/include/stdio.h
 
 >如果使用""代表會先從當前檔案所在的資料夾下面查詢
 >如果在當前檔案所在的資料夾下面沒有找到, 那麼就回去開發工具的編譯環境中去查詢
 >如果編譯環境中沒有找到, 那麼會再去系統的編譯環境中找
 
 注意: include後面不一定要寫檔名稱 也可以寫路徑(相對路徑/ 全路徑)
 
 */

main.c

#include <stdio.h>
#include "lisi.h"

int main(int argc, const char * argv[]) {
   
    printf("%i\n", sum(10, 20));
    printf("%i\n", average(10, 20));
    printf("%i\n", test(10, 20));
    printf("%i\n", new1(998));
    
    return 0;
}

lisi.c

#include <stdio.h>

int sum(int v1, int v2)
{
    return (v1  + v2) * 10;
}

int average(int v1, int v2)
{
    return (v1 + v2) / 2;
}

int test(int v1, int v2)
{
    return v1 * v1 + v2 * v2 + v1 + v2 * v1;
}

int new1(int v1)
{
    return v1;
}

lisi.h

// 注意: .h是專門用來被拷貝的, 不會參與編譯

#ifndef day05_lisi_h
#define day05_lisi_h
// 計算兩個使用者和
int sum(int v1, int v2);
// 計算兩個使用者的平均值
int average(int v1, int v2);
// 計算兩個使用者一年的費用
int test(int v1, int v2);
// 獲取電量
int new1(int v1);
#endif