Series: C++
cpp
39 lines
· Updated 2026-04-03
hello_world.cpp
C++/Part1_基礎入門/Ch01_認識CPP與開發環境/hello_world.cpp
// ============================================================
// Ch01 - Hello World 程式
// 編譯:g++ -std=c++17 -Wall -o hello_world hello_world.cpp
// 執行:./hello_world
// ============================================================
// #include 是前置處理指令,用來引入標頭檔
// <iostream> 提供輸入輸出功能,包含 std::cout 和 std::cin
#include <iostream>
// main() 是每個 C++ 程式的進入點
// 作業系統會呼叫這個函式來啟動程式
// int 表示此函式會回傳一個整數
int main() {
// std::cout 是標準輸出物件(console output)
// << 是輸出運算子,把右邊的資料送到左邊的串流
// "Hello, World!" 是字串字面值(string literal)
// std::endl 會輸出換行字元並清空輸出緩衝區
std::cout << "Hello, World!" << std::endl;
// 也可以用 '\n' 換行(效能稍好,不會清空緩衝區)
std::cout << "歡迎來到 C++ 的世界!\n";
// 可以在一行中串接多個 << 輸出不同內容
std::cout << "這是" << "第一個" << " C++ 程式" << std::endl;
// 輸出數字也可以直接使用 <<
std::cout << "1 + 1 = " << 1 + 1 << std::endl;
// return 0 表示程式正常結束
// 非零值通常表示發生錯誤
return 0;
}
// 預期輸出:
// Hello, World!
// 歡迎來到 C++ 的世界!
// 這是第一個 C++ 程式
// 1 + 1 = 2
Related articles
C++
c
Updated 2026-07-21
deviceAlpha.h
deviceAlpha.h — c source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/deviceAlpha.h).
Read article →
C++
c
Updated 2026-07-21
finalproject.c
finalproject.c — c source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/finalproject.c).
Read article →
C++
cpp
Updated 2026-07-21
finalproject.cpp
finalproject.cpp — cpp source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/finalproject.cpp).
Read article →
C++
c
Updated 2026-07-21
deviceAlpha.h
deviceAlpha.h — c source code from the C++ learning materials (C++/Mavis_Homework/Lab8/deviceAlpha.h).
Read article →
C++
c
Updated 2026-07-21
lab8.c
lab8.c — c source code from the C++ learning materials (C++/Mavis_Homework/Lab8/lab8.c).
Read article →
C++
cpp
Updated 2026-07-21
lab8.cpp
lab8.cpp — cpp source code from the C++ learning materials (C++/Mavis_Homework/Lab8/lab8.cpp).
Read article →