CH01_初識(shí)C++
第一個(gè)C++程序
新建項(xiàng)目


新建文件

編寫(xiě)代碼
#include <iostream>
using namespace std;
int main() {
cout << "hello world" << endl;
system("pause");
return 0;
}
運(yùn)行程序

注釋
作用:在代碼中加一些說(shuō)明和解釋,方便閱讀代碼。
兩種格式:
單行注釋: //這是注釋
多行注釋: /這是注釋/
#include <iostream>
using namespace std;
//入口函數(shù)
int main() {
//控制臺(tái)輸出
cout << "hello world" << endl;
/*
cout << "hello world" << endl;
cout << "hello world" << endl;
cout << "hello world" << endl;
*/
system("pause");
return 0;
}
變量
作用:給一段指定的內(nèi)存空間起名,方便操作這段內(nèi)存。
語(yǔ)法:數(shù)據(jù)類型 變量名=初始值;
示例:
#include <iostream>
using namespace std;
//入口函數(shù)
int main() {
//定義變量
int num = 10;
//控制臺(tái)輸出
cout << "數(shù)字:" << num << endl;
system("pause");
return 0;
}
常量
作用:用于記錄程序中不可更改的數(shù)據(jù)
語(yǔ)法:
1.#define 宏常量:#define 常量名 常量值
2.const修飾的變量:const 數(shù)據(jù)類型 常量名=常量值
示例:
#include <iostream>
using namespace std;
//宏常量
#define day 7
//入口函數(shù)
int main() {
//常規(guī)常量
const double pai = 3.14;
//更改常量的值會(huì)報(bào)錯(cuò)
//day = 8;
//pai = 3.5
//控制臺(tái)輸出
cout << "每周天數(shù):" << day << endl;
cout << "圓周率:" << pai << endl;
system("pause");
return 0;
}
關(guān)鍵字
作用:關(guān)鍵字是C++中預(yù)先保留的單詞(標(biāo)識(shí)符)

提示:定義標(biāo)識(shí)符時(shí),禁止使用關(guān)鍵詞,否則會(huì)報(bào)錯(cuò)。
示例:
#include <iostream>
using namespace std;
//入口函數(shù)
int main() {
//標(biāo)識(shí)符使用關(guān)鍵字會(huì)報(bào)錯(cuò)
int int = 5;
//控制臺(tái)輸出
cout << "變量:" << int << endl;
system("pause");
return 0;
}
標(biāo)識(shí)符
作用:C++規(guī)定給標(biāo)識(shí)符(變量,常量等)命名時(shí),有一套自己的規(guī)則
命名規(guī)則:
1.不能是關(guān)鍵字
2.只能由字母,數(shù)字,下劃線組成
3.不能以數(shù)字開(kāi)頭
4.字母區(qū)分大小寫(xiě)
示例:
#include <iostream>
using namespace std;
//入口函數(shù)
int main() {
//正確定義
int num_1 = 5;
int a = 5;
int A = 5;
//錯(cuò)誤定義
int int = 5;
int 3num = 5;
int a&b = 5;
system("pause");
return 0;
}
浙公網(wǎng)安備 33010602011771號(hào)