程式碼 :
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
cout<<"Hello World!!"<<endl;
cout<<"This is the first program"<<endl;
return 0;
}
程式碼內容介紹 :
#include <iostream>
#include 是前置處理器指令,它告訴編譯器這個程式必須使用到 iostream 這個表頭檔(Header file)中的函式(Function)與類別(Class)定義。
using namespace std;
using 指令表示使用 std 名稱空間(Namespace)下的名稱,例如 cout,如果不寫這行的話,要使用 cout 就必須寫為 std::cout。
int main(int argc, char** argv)
{
.... // 中間指令
return 0;
}
主函式(Main function)為主要執行指令和其他函式的地方,其中 int 表示函式執行完畢後會回傳整數(Integer),括號的位置可設置參數列,括號中如果不設定任何參數,表示程式不接收任何引數。
// 代表單行註解 ( 註解會寫在 // 的後面 ),/**/ 代表多行註解 ( 註解會寫在 /* 和 */ 的中面 )。
程式的最後一行是 return 0,也就是程式結束後回傳 0,在 main 函式中,回傳 0 表示程式正常結束。
cout<<"Hello World!!"<<endl;
cout<<"This is the first program"<<endl;
cout 是 C++ 開啟的輸出串流(istream)物件,<< 是輸出運算子(Operator),雙引號 "" 的中間是想輸出的字串,而 endl 是換行,C++ 每個陳述結束都必須使用分號 ( ; ),這樣才是完整的指令。
輸出結果 :
Hello World!!
This is the first program

請先 登入 以發表留言。