Xcode 是 macOS 上最強大的整合開發環境 (IDE),雖然主攻 Swift/Objective-C,但完整支援 C/C++ 開發。本文圖文並茂教你建立第一個 C 語言命令列專案並執行互動式輸入輸出。
HelloC)com.example)main.c:自動產生的入口檔案,已包含 #include <stdio.h> 與 main 函式。#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
int number;
printf("please enter number: ");
scanf("%d", &number);
// 插入你的程式碼...
printf("you are enter number is: %d\n", number);
return 0;
}
點擊左上角 ▶ (Play) 按鈕 或按 Cmd + R,Xcode 會自動編譯、連結並執行。
cd 專案資料夾
clang main.c -o myprogram
./myprogram
please enter number: 12
you are enter number is: 12
| 問題 | 解決方案 |
|---|---|
| 找不到 main.c | 確認 Language 選 C,非 Swift/C++ |
| scanf 無法輸入 | Xcode 底部輸出區點擊游標啟用輸入 |
| 想用 C99/C11 標準 | Build Settings → C Language Dialect 選擇 GNU99/GNU11 |
| 需引用數學庫 | Build Settings → Other Linker Flags 加入 -lm |
| 除錯斷點不生效 | Scheme → Run → Info → Build Configuration 設為 Debug |
xcode-select --install,獲得 gcc/clang/make 等工具。Cmd+Shift+Y 開啟主控台)。Source Control 選單即可提交、推送。小結:Xcode 提供優秀的編輯、編譯、除錯一體化體驗。習慣後,你會發現它比純文字編輯器+終端機更高效,尤其對大型專案管理優勢明顯。