/* 1. 將下面程式拆成三個檔案 sub.h, sub.c, main.c 要記得把 sub.c 和 main.c 裡面的 #include "sub.h" 打開 2. 分開編譯並檢查看看新產生的 sub.o 與 main.o 是什麼類型的檔案: gcc -Wall -c sub.c gcc -Wall -c main.c file *.o 3. 聯結並檢查新產生的 main 是什麼類型的檔案: gcc -o main sub.o main.o -lncurses file main 4. 執行: ./main */ /* sub.h =========================================================== */ extern void withdraw(int x); extern void deposit(int x); extern int f(int n); extern int _balance_; /* sub.c =========================================================== */ /* #include "sub.h" */ #include int _balance_ = 0; void withdraw(int x) { static int count = 0; _balance_ -= x; printf("%2d %4d %4d\n", ++count, -x, _balance_); } void deposit(int x) { static int count = 0; _balance_ += x; printf(" %2d %4d %4d\n", ++count, x, _balance_); } int f(int n) { return n>1 ? n*f(n-1) : 1; } /* main.c ========================================================== */ /* #include "sub.h" */ #include #include #include #include int main(void) { int *p; initscr(); clear(); mvaddstr(0, 0, "Hello!"); mvaddstr(1, 0, "Now I will sleep for 3 seconds"); refresh(); sleep(3); endwin(); printf("original balance: %d\n", _balance_); deposit(20); deposit(30); p = (int *) malloc(sizeof(int)); *p = f(4); withdraw(50); deposit(30); deposit(*p); free(p); withdraw(14); printf("final balance: %d\n", _balance_); return 0; } /* 作業: 以下各題獨立. 每做一題之前, 請務必先將程式原始碼還原, 並清除先前所產生的 .o 及可執行檔! 1. 若將兩句 #include "sub.h" 刪除掉, 可成功地產生那些檔案? 何時會出現錯誤訊息? 請翻譯該錯誤訊息. 2. 若將 #include 刪除掉, 可成功地產生那些檔案? 何時會出現錯誤訊息? 請翻譯該錯誤訊息. 3. 若將聯結時的 -lncurses 刪除掉, 會出現什麼錯誤訊息? 請翻譯. 4. 若將 int _balance_ = 0; 這句刪除掉, 可成功地產生那些檔案? 何時會出現錯誤訊息? 請翻譯. 5. 若將 extern int _balance_; 這句刪除掉, 但把 int _balance_ = 0; 這句搬來這裡取代它, 可成功地產生那些檔案? 何時會出現錯誤訊息? 請翻譯. 6. 若在 int _balance_ = 0; 這句前面加上 static, 可成功地產生那些檔案? 何時會出現錯誤訊息? 請翻譯. 7. 若將兩句 static int count = 0; 當中的 static 拿掉, 會有何效果? */