自学C++学习记录
01变量的使用
变量存在的意义:方便管理内存空间
数据类型 数据名=变量初始值
#include<iostream>
using namespace std;
int main() {
//变量创建的语法
int a = 10;
cout << "a=" << a << endl;
system("pause");
return 0;
}
```cpp
02常量
function:记录程序中不可更改的数据
两种常量的表示方法
1.#define 一般定义在文件的上方
2. const修饰的变量=常量
#include<iostream>
using namespace std;
//常量的定义方式
//1.#define 宏常量
//2.const 修饰的变量
#define Day 7
int main() {
//Day = 14;//Day是个常量,一旦修改就会报错
cout << "一周总共有:" << Day << "天"<<endl;
//2.const 修饰的变量
const int month = 12;
// month = 21; const 修饰的变量也称为常量
cout << "一年有多少个" << month << "月份" << endl; //记住这个表示方式
system("pause");
return 0;
}
代码显示结果为:
一周总共有:7天
一年有多少个12月份
03关键字
key word: c++预先保留的单词
#include<iostream>
using namespace std;
#define Day 7
int main() {
//创建变量
//不要用关键字 给变量和常量起名
int int a = 10; 第二个int 为关键字,起名后会产生歧义
system("pause");
return 0;
}
04标识符命名规则
C++规定给标识符(变量、常量)命名的时候,规则
(1)标识符不可以是关键字
(2)字母+数字+下划线
(3)首字符 只能为 字母、下划线
(4)标识符区分大小写
#include<iostream>
using namespace std;
int main() {
//(1)标识符不可以是关键字
//int int = 10; //错的
//(2)字母 + 数字 + 下划线
int a1_ = 10;
//(3)首字符 只能为 字母、下划线
int y2d=0;
//
//(4)标识符区分大小写
int aa = 10;
cout << aa << endl;
//cout << AA << endl;
//建议给变量起名的时候;最好能够见名知道意思
int num1 = 10;
system("pause");
return 0;
}
05数据类型
5.1整型
数据类型的存在的意义:给变量分配合适的空间。
:
项目 | 占用字节 |
---|---|
short(短整型) | 2字节 |
int (整型) | 4字节 |
long(长整型) | windows 4字节 Linux为4字节(32位)8字节(64位) |
long long(长整型) | 8字节 |
所占空间不同
5.2sizeof
sizeof 的作用:统计数据类型的内存空间
#include<iostream>
using namespace std;
//sizeof 求出数据类型
int main() {
short num1 = 10;//2*15
cout <<"short占用的内存空间为:"<< sizeof(short) << endl;
int num2 = 10;
cout << "short占用的内存空间为:" << sizeof(int) << endl;
long num3 = 10;
cout << "short占用的内存空间为:" << sizeof(num3) << endl;
long long num4 = 10;
cout << "short占用的内存空间为:" << sizeof(num4) << endl;
cout << num2 << endl;
system("pause");
return 0;
}
5.3实型(浮点型)
float 4个字节 7有效数字
double 8字节 15-16有效数字
#include<iostream>
using namespace std;
//sizeof 求出数据类型
int main() {
float f1 = 3.1415926f;
cout << "f1=" << f1 << endl;
double f2 = 3.1415926;
cout << "f2=" << f2 << endl;
//统计float和double 所占的内存空间
cout << "float所占的内存空间为:" << sizeof(float) << "字节" << endl;
cout << "double所占的内存空间为:" << sizeof(double) << "字节" << endl;
system("pause");
return 0;
5.4字符型
#include<iostream>
using namespace std;
//sizeof 求出数据类型
int main() {
//1.创建方式
char ch = 'a';//字符型变量只占用1个字符,把相对应的ASCII码放入相对应的存储单元
cout << ch << endl;
cout << "字符型所占的内存空间为:" << sizeof(char) << "字节" << endl;
//char ch2 = "b";//使用单引号
//char ch = 'ddsadsefdf'; 单引号类里面只能有一个字符
cout << "double所占的内存空间为:" << sizeof(double) << "字节" << endl;
//ASCII码 a-97 A-65
cout << int(ch) << endl;
system("pause");
return 0;
}
5.5转义字符
\n 换行
\ 输出
\t 水平制表符 ;功能::多行输出时候,整齐的输出数据
#include<iostream>
using namespace std;
//sizeof 求出数据类型
int main() {
//1.转义字符 \n换行
cout << "hello world\n" ;
//1.转义字符 \\反斜杠
cout << "\\"<<endl;
//1.转义字符 \t 水平制表符 ;功能::多行输出时候,整齐的输出数据
cout << "aaaaasghtt\thello world" << endl;
cout << "aaaaaefs\thello world" << endl;
cout << "aaaaaferer\thello world" << endl;
system("pause");
return 0;
}
5.6字符串型
#include<iostream>
#include<string>//使用C++风格的字符串 ,必须先加上sting 头文件
using namespace std;
int main() {
//1.C风格 char 变量名[]="abcd";
char str[] = "hello world";
cout << str<<endl;
//2.C++风格的字符串。记得加头文件<string>
string str2 = "hello world";
cout << str2<<endl;
system("pause");
return 0;
}
5.7布尔类型BOOL
布尔类型的表示数据的真假
#include<iostream>
using namespace std;
int main() {
//1.;bool字符表示真贱
bool flag = true;
cout << flag<<endl;
flag = false;
cout << flag << endl;
cout << "查看布尔类型的字符内存为:" << sizeof(bool) << endl;
system("pause");
return 0;
}
5.8输入输出
#include<iostream>
using namespace std;
int main() {
//整数型
int a = 0;
cout << "请给整数型变量a赋值:" << endl;
cin >> a;
cout << "整数型变量a=" << a<<endl;
//浮点型
float b = 43.1415f;
cout << "请给浮点型变量b赋值:" << endl;
cin >> b;
cout << "浮点型型变量b=" << b << endl;
system("pause");
return 0;
}
5.8运算符
取模运算就是求余数
#include<iostream>
using namespace std;
int main() {
//int a = 10;
//int b = 3;
//cout << a / b << endl;//结果为3,去除掉了小数部分
取模运算
//cout << a % b << endl;
两个小数之间是不可以做取模运算的
//float s = 3.14; float c = 0.11;
cout << s % c << endl;
//递增递减运算符
//(1)前置递增
int a = 10;
++a;//变量+1
cout << "a=" << a << endl;
//(2)后置递增
int b= 10;
b++;//变量+1
cout << "a=" << a << endl;
//(3)前置递增后置递增的区别
//前置递增,先变量+1,再进行表达式运算
int a1 = 10;
int b1 = ++a1 * 10;
cout << a1 << endl;
cout << b1 << endl;
//后置递增,先表达式运算,再变量+1
int a2 = 10;
int b2 = a2++ * 10;
cout << a2 << endl;
cout << b2 << endl;
//前置递减 先变量后自减运算
int d = 10;
int f = --d + 10;
cout << d << endl;
cout << f << endl;
//后置递减 先运算 后变量自减
int d1 = 10;
int f1 = d1-- +10;
cout << d1 << endl;
cout << f1 << endl;
system("pause");
return 0;
}
5.9逻辑运算符
非 !
与 &&
或 ||
#include<iostream>
using namespace std;
//比较运算符
int main() {
//==
int a = 0;
int b = 0;
cout << (a >b) << endl;
cout << !!a << endl;
//a&&b a,b均为真则为真,ab均为假则为假
cout << (a || b) << endl;
system("pause");
return 0;
}