文章目录
一、第一个C++程序
用C++书写hello world
示例
#include <iostream> //固定语句
using namespace std; //固定语句
//1、单行注释
/* 2、多行注释
main是一个程序的入口
每个程序只能有一个main函数
*/
int main()
{
//在屏幕中输出hello world
cout << "hello world" << endl;
system("pause"); //固定语句
return 0; //固定语句
}
二、程序的注释
1、单行注释 :// 注释内容
2、多行注释 : /* 注释内容*/
三、变量
1、变量存在的意义: 方便我们管理内存空间
2、变量创建的语法: 数据类型 变量名 = 变量初始值 (int a =10;)
示例
#include <iostream>
using namespace std;
int main()
{
//定义一个变量
int a = 10;
cout << "a = "<< a << endl;
system("pause");
return 0;
}
四、 常量
**1、常量存在的意义 :**用于记录程序中不可更改的数据
2、 C++定义常量的两种方式
(1)#define 宏常量 #define 常量名 常量值
== 通常在文件上方定义== ,表示一个常量
**(2)const 修饰的变量 **const 数据类型 常量名 = 常量值
== 通常在变量定义前加关键字const == ,修饰该变量为常量,不可修改
示例
#include<iostream>
using namespace std;
//常量的定义
//1.#define 宏常量
#define Day 7
int main()
{
//Day = 10; 此处为错误,前面用define 定义后,不可再次更改
cout << "一周总共有" << Day <<"天"<<endl;
const int month = 12;
//const = 13 ;此处错误,用const定义后,其值不可再次修改
cout << "一年有" << month << "月" << endl;
system("pause");
return 0;
}
五、关键字
不要用关键字给变量或常量起名字
六、标识符命名规则
**作用:**c++规定给标识符(变量、常量)命名时,有一套自己的规则
-
标识符不能是关键字
-
标识符只能由字母、数字、下划线组成
-
第一个字符必须为字母或下划线
-
标识符中字母区分大小写
七、数据类型
C++规定在创建一个变量或者常量时,必须要指定出相应的数据类型,否则无法给变量分配内存
7.1 整型
1、短整型 (short) 占用两字节
2、整型 (int) 占用四字节
3、长整型 (long)Windows 占用四字节
4、长长整型(long long)占用八字节
7.2 sizeof 关键字
利用sizeof 关键字可以 == 统计数据类型所占内存大小==
语法 :sizeof(数据类型 / 变量)
【示例】
#include<iostream>
using namespace std;
int main()
{
//整型 :short (2) int (4) long (4) long long (8)
short num1 = 10;
cout << "short占用的内存空间为" << sizeof(short) << endl;
cout << "short占用的内存空间为" << sizeof(num1) << endl;
int num2 = 10;
cout << "int占用的内存空间为" << sizeof(int) << endl;
cout << "int占用的内存空间为" << sizeof(num2) << endl;
long num3 = 10;
cout << "long占用的内存空间为" << sizeof(long) << endl;
cout << "long占用的内存空间为" << sizeof(num3) << endl;
long long num4 = 10;
cout << "long long占用的内存空间为" << sizeof(long long) << endl;
cout << "long long占用的内存空间为" << sizeof(num4) << endl;
system("pause");
return 0;
}
结果
7.3 实型
表示小数
1、单精度:float 占用四字节,可以统计7位有效数字
2、双精度:double 占用八字节,可以统计15~16位有效数字
示例
#include<iostream>
using namespace std;
//两种实型输出
int main()
{
float f1 = 3.14529265f;
cout << "f1 = " << f1 << endl;
double d1 = 3.14159265;
cout << "d1 = " << d1 << endl;
//科学计数法
float f2 = 3e2; //3*10^2
float f3 = 2e-2; //2*0.1^2
cout << "f2 = " << f2 << endl;
cout << "f3 = " << f3 << endl;
system("pause");
return 0;
}
输出结果
【注】 在C++中,输出小数时,默认输出6位有效数字
7.4 字符型
显示单个字符 ==
语法: char ch = ‘a’ ; (单引号中只能有一个字符==)
示例
#include<iostream>
using namespace std;
int main()
{
//1、字符型变量创建方式
char ch = 'a';
cout << ch << endl;
//2、字符型变量所占用内存大小
cout <<"char型占用内存:"<< sizeof(char) << endl;
//3、字符型变量对应ASCII编码
cout << "a对应的ASCII编码:"<< (int)ch << endl;
system("pause");
return 0;
}
输出结果
7.5 转义字符
\n 换行 ,将当前位置移到下一行开头
在C++中,endl也表示换行
\t 水平制表(跳到下一个TAB位置),一个\t 是八个字符
\ 代表一个反斜线字符 ‘ \ ’
示例
#include<iostream>
using namespace std;
int main()
{
//换行
cout << "hello world\n";
//制表
cout << "a\tdivition" << endl;
cout << "aa\tdivition" << endl;
cout << "aaa\tdivition" << endl;
cout << "aaaa\tdivition" << endl;
//单斜杠
cout << "\\" << endl;
system("pause");
return 0;
}
输出结果
7.6 字符串型
1、C风格字符串 : char 变量名[ ] = “字符串值”
2、C++风格字符串 : string 变量名 = “字符串值”
示例
#include<iostream>
#include<string> //若用C++风格定义字符串,则需添加此头文件
using namespace std;
int main()
{
//1、C风格
char str1[] = "hello world" ;
cout << str1 << endl;
//2、C++风格
string str2 = "hello world";
cout << str2 << endl;
system("pause");
return 0;
}
7.7 布尔类型 bool
布尔数据类型代表真或假的值
-
true 真(本质是1)
-
false 假(本质是0)
示例
#include<iostream>
using namespace std;
int main()
{
//1、创建bool类型
bool flag = false;
cout << "结果为假时输出:" << flag << endl;
flag = true;
cout << "结果为真时输出:" << flag << endl;
//2、查看bool类型所占内存空间
cout << "bool类型所占内存空间为:" << sizeof(bool) << endl;
system("pause");
return 0;
}
输出结果
7.8 数据的输入
用于从键盘获取数据
关键字 :cin
语法: cin >> 变量
示例
#include<iostream>
using namespace std;
int main()
{
//1、整型
int a = 0;
cout << "请给整型变量a赋值:" << endl;
cin >> a;
cout<<"整型变量a = "<<a<<endl;
//2、浮点型
//3、字符型
//4、字符串型
//5、布尔类型
system("pause");
return 0;
}
输出结果
【注释】 学习课程为-黑马程序C++教程