시리즈: C++
cpp
106 줄
· 업데이트 2026-04-03
basic_output.cpp
C++/Part1_基礎入門/Ch01_認識CPP與開發環境/basic_output.cpp
// ============================================================
// Ch01 - std::cout 輸出示範
// 編譯:g++ -std=c++17 -Wall -o basic_output basic_output.cpp
// 執行:./basic_output
// ============================================================
#include <iostream>
int main() {
// ── 1. 基本字串輸出 ──
std::cout << "=== 基本字串輸出 ===" << std::endl;
std::cout << "Hello, C++!" << std::endl;
std::cout << "你好,C++!" << std::endl;
// ── 2. 輸出數字 ──
std::cout << "\n=== 輸出數字 ===" << std::endl;
std::cout << "整數:" << 42 << std::endl;
std::cout << "負數:" << -7 << std::endl;
std::cout << "浮點數:" << 3.14159 << std::endl;
std::cout << "運算結果:10 / 3 = " << 10.0 / 3.0 << std::endl;
// ── 3. 串接多個 << 運算子 ──
std::cout << "\n=== 串接輸出 ===" << std::endl;
// 可以連續使用 << 輸出不同型別的資料
std::cout << "姓名:" << "小明" << ",年齡:" << 20 << ",身高:" << 175.5 << " cm" << std::endl;
// 也可以拆成多行撰寫(提高可讀性)
std::cout << "今天是 "
<< 2025 << " 年 "
<< 1 << " 月 "
<< 1 << " 日"
<< std::endl;
// ── 4. 換行方式比較:\n vs std::endl ──
std::cout << "\n=== 換行方式 ===" << std::endl;
// \n:僅插入換行字元,效能較好
std::cout << "第一行\n";
std::cout << "第二行\n";
// std::endl:插入換行字元 + 清空(flush)輸出緩衝區
// 在需要確保輸出立即顯示時使用(如除錯)
std::cout << "第三行" << std::endl;
std::cout << "第四行" << std::endl;
// ── 5. 跳脫字元(Escape Characters)──
std::cout << "\n=== 跳脫字元 ===" << std::endl;
// \n — 換行
std::cout << "第一行\n第二行\n第三行\n";
// \t — Tab(水平定位)
std::cout << "姓名\t年齡\t城市" << std::endl;
std::cout << "小明\t20\t台北" << std::endl;
std::cout << "小華\t22\t高雄" << std::endl;
// \\ — 輸出反斜線本身
std::cout << "檔案路徑:C:\\Users\\Documents\\file.txt" << std::endl;
// \" — 在字串中輸出雙引號
std::cout << "他說:\"C++ 很有趣!\"" << std::endl;
// \' — 輸出單引號(在字串中可直接寫 ',但字元中需要 \')
std::cout << "It\'s a beautiful day!" << std::endl;
// \a — 警告音(嗶聲,部分終端機支援)
// std::cout << "\a"; // 取消註解可測試
// \r — 歸位(回到行首,覆蓋原有內容)
// std::cout << "ABCDE\rXY"; // 輸出 XYCDE
// \0 — 空字元(null character),字串結尾
// 一般不需要手動使用
// ── 6. 常見跳脫字元總表 ──
std::cout << "\n=== 跳脫字元總表 ===" << std::endl;
std::cout << "\\n\t換行(Newline)" << std::endl;
std::cout << "\\t\tTab(水平定位)" << std::endl;
std::cout << "\\\\\t反斜線(Backslash)" << std::endl;
std::cout << "\\\"\t雙引號(Double Quote)" << std::endl;
std::cout << "\\'\t單引號(Single Quote)" << std::endl;
std::cout << "\\0\t空字元(Null Character)" << std::endl;
std::cout << "\\a\t警告音(Alert)" << std::endl;
std::cout << "\\r\t歸位(Carriage Return)" << std::endl;
// ── 7. 實用範例:格式化輸出 ──
std::cout << "\n=== 格式化輸出範例 ===" << std::endl;
// 簡易表格
std::cout << "+-----------+-------+--------+" << std::endl;
std::cout << "| 品名 | 數量 | 單價 |" << std::endl;
std::cout << "+-----------+-------+--------+" << std::endl;
std::cout << "| 蘋果 | 3 | 25 |" << std::endl;
std::cout << "| 香蕉 | 5 | 15 |" << std::endl;
std::cout << "| 橘子 | 2 | 30 |" << std::endl;
std::cout << "+-----------+-------+--------+" << std::endl;
// 使用 Tab 對齊
std::cout << "\n使用 Tab 對齊:" << std::endl;
std::cout << "科目\t\t成績" << std::endl;
std::cout << "國文\t\t85" << std::endl;
std::cout << "英文\t\t92" << std::endl;
std::cout << "數學\t\t78" << std::endl;
return 0;
}
관련 글
C++
c
업데이트 2026-07-21
deviceAlpha.h
deviceAlpha.h — c source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/deviceAlpha.h).
글 읽기 →
C++
c
업데이트 2026-07-21
finalproject.c
finalproject.c — c source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/finalproject.c).
글 읽기 →
C++
cpp
업데이트 2026-07-21
finalproject.cpp
finalproject.cpp — cpp source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/finalproject.cpp).
글 읽기 →
C++
c
업데이트 2026-07-21
deviceAlpha.h
deviceAlpha.h — c source code from the C++ learning materials (C++/Mavis_Homework/Lab8/deviceAlpha.h).
글 읽기 →
C++
c
업데이트 2026-07-21
lab8.c
lab8.c — c source code from the C++ learning materials (C++/Mavis_Homework/Lab8/lab8.c).
글 읽기 →
C++
cpp
업데이트 2026-07-21
lab8.cpp
lab8.cpp — cpp source code from the C++ learning materials (C++/Mavis_Homework/Lab8/lab8.cpp).
글 읽기 →