Để chạy Hello World, làm theo các bước:
1. Viết chương trình
Tạo file hello.cpp:
#include <iostream>
int main() {
std::cout << "Hello World\n";
}
Phân tích từng dòng
| Dòng | Giải thích |
|---|---|
| 1 | #include <iostream> – Preprocessor copy header iostream (quản lý I/O stream) vào source. Cho phép input/output chuẩn. |
| 2 | Dòng trống – Không ảnh hưởng. |
| 3 | int main() { – Hàm main, entry point, trả về int. Mở khối {. |
| 4 | std::cout << "Hello World\n"; – std::cout (console output) + << (insertion operator) + chuỗi. Kết thúc bằng ;. |
| 5 | } – Đóng khối main. |
2. Biên dịch
Terminal → cd đến thư mục file → g++ -o hello hello.cpp (tạo executable hello).
3. Chạy
./hello (Linux/macOS) hoặc hello.exe (Windows)
Kết quả
Hello World