代碼拆分解釋
例子:
#include<iostream>
using namespace std;
int main()
{
cout<<"hello world"<<endl;
}
#號(hào)是指預(yù)處理指令,
include指令不是必須的
iostream: io: input &output stream 流:東西不是打印出來(lái)的,而是通過(guò)字節(jié)流棠涮,流到一個(gè)管道里去。
<<:流到cout
iostream包含了關(guān)于輸入輸出的函數(shù)的語(yǔ)句怪蔑,#include<iostream>是指將iostream文件的內(nèi)容添加到程序中枕扫。
Screenshot 2019-07-20 at 20.42.20.png
#include<stdio.h>
printf("HelloWorld\n");
#include<cstdio>
加c是新版本,.h是老版本
老版本不用加namespace 命名空間
using namespace std; 使用命名空間std (standard)
如果不寫using namespace std畜埋,那么在輸入輸出的時(shí)候?qū)?code>std::cout<<"Hello"<<std::endl;
cout<<"hello world"<<endl;
cout<<"hello"<<" world"<<endl;
<< 是插入運(yùn)算符莫绣,是指將“hello world”插入到輸出流,
endl: endline 是控制符悠鞍,表示重啟一行\(zhòng)n
endl 與\n的區(qū)別:
endl 1. 換行对室,2. 確保程序立即輸出
\n 換行
#include<cmath>
#include <iostream>
using namespace std;
int main()
{
//cout: 輸出對(duì)象
cout <<"英雄名稱:寒冰射手艾希"<<endl;
cout<<"傷害:56\t 攻擊距離:600"<<endl;
cout <<"護(hù)甲:15.5\t 魔抗:30(+0.0)"<<endl
<< "生命值:395\t生命恢復(fù):0.9"<<endl;
cout <<"護(hù)甲:15.5\t 魔抗:30(+0.0)\n"
<< "生命值:395\t生命恢復(fù):0.9\n";
cout<<INT_MAX<<endl;
return 0;
}
編碼規(guī)范
- 每條語(yǔ)句獨(dú)占一行
- 花括號(hào)獨(dú)占一行
- 縮進(jìn)
- 與函數(shù)名稱相關(guān)的小括號(hào)周圍沒(méi)有空白
- 單條注釋以//開(kāi)頭,多條注釋/.../
c++的編譯和執(zhí)行
預(yù)處理-編譯-執(zhí)行
g++ helloworld.cpp 預(yù)處理
g++ helloworld.cpp -o helloworld 預(yù)處理加編譯編譯
./helloworld 執(zhí)行
hello, world