Skip to content

[C 語言] 使用 gcc 指令編譯,並印出 Hello World

C 語言是最常見的程式語言之一,能夠在許多 Unix 系統中瞧見它的身影,許多系統與程式也都是使用 C 語言來進行開發。

所以 C 語言可說是程式開發人員熟悉起來絕對沒有壞處的程式語言。

以下的環境以 LinuxMacOS 等作業系統來編譯、執行程式。若是在 Windows 環境下,請參考:MSYS2


印出 Hello World

學習一個新的程式語言,自然是從印出 Hello World 字串開始。

首先先製作一份叫做 hello.c 的文件。

#include <stdio.h>


int main() {
    printf("Hello World\n");
    return 0;
}


接著,在終端機中使用 gcc 指令編譯。

gcc hello.c

這時應該會在當前目錄底下發現一個名叫 a.out 的檔案,這也就是我們編譯後的執行檔。

同樣在終端機中使用以下指令執行:

./a.out

Output:

Hello World


補充說明

#include <stdio.h>            // include information about standard library


int main() {                  // define a function named "main"
    printf("Hello World\n");  // statements of main to print this sequence of characters
    return 0;                 // It means this program end normally
}



References


Read More

Tags:

Leave a Reply