类型

#include <iostream> // 预处理int main(int argc, char const *argv[]){using namespace std; // 引入命名空间auto a = 3; // intauto b = 3.3; // doubleauto c = 3.4f; // floatauto d = 3.5l; // long doublecout << sizeof(a) << endl; // 4cout << sizeof(b) << endl; // 8cout << sizeof(c) << endl; // 4cout << sizeof(d) << endl; // 8cout << 2E+2 << endl; // 2 * (10 ^ 2) = 200cout << 2E-2 << endl; // 2 / (-10 ^ 2) = 0.02char ch = 'a'; // 字符char str[] = "abcdefg"; // 字符 , 7字节,要加上\0cout << ch << endl;cout << str << endl;cout << sizeof(ch) << endl; // 1字节cout << sizeof(str) << endl; // 8字节 要加上 \0cout << int(ch) << endl; // 强制转换 97getchar();/* code */return 0;}
常量
/*1.#define 宏常量:常量名 常量值2. const 数据类型 常量名 = 常量值*/#define a 11const int b 12;
例子
#include <iostream> // 预处理#define a 11int main(int argc, char const *argv[]){using namespace std; // 引入命名空间cout << "hello world! lijunyang" << endl;int i = getchar();system("ls -al");const int b = 12;cout << a << "-" << b << endl;cout << b << endl;/* code */return 0;}
标识符命名规则
标识符不可以是关键字
标识符只能由字母,数字,下划线
第一个字符必须是字母或下划线
标识符区分大小写
sizeof
// sizeof(数据类型 或 变量)#include <iostream> // 预处理int main(int argc, char const *argv[]){using namespace std; // 引入命名空间cout << sizeof(int) << endl; // 返回 4 表示 4字节int a[20];cout << sizeof(a) << endl; // 返回 80 表示 80字节getchar();/* code */return 0;}
转译字符
| 转译字符 | 含义 | ascii码值(十进制) |
|---|---|---|
| \a | 警报 | 007 |
| \b | 退格(BS),将当前位置移到前一列 | 008 |
| \f | 换页(FF),将当前位置移到下一页开头 | 012 |
| \n | 换行符 | 010 |
| \r | 回车符 | 013 |
| \t | 水平制表符 | 009 |
| \v | 垂直制表符 | 011 |
| \\ | \ | 092 |
| \‘ | ‘ | 039 |
| \“ | “ | 034 |
| \? | ? | 063 |
| \0 | 0 | 000 |
| \ddd | 八进制转译符号,d范围0-7 | 3位八进制 |
| \xhh | 16进制转译符号,h范围0~9,a~f,A~F | 3位十六进制 |
字符串表示
#include <iostream> // 预处理#include <string> // 引入这个才能使用string类型int main(int argc, char const *argv[]){using namespace std; // 引入命名空间char str1[] = "hello world";string str2 = "hello world";/* code */return 0;}
布尔类型
int main() {bool flag = true;cout << flag << endl; // 1;flag = false;cout << flag << endl; // 0cout << sizeof(flag) << endl; // 1getchar();return 0;}
cin
#include <iostream> // 预处理#include <string> // 引入这个才能使用string类型int main(){using namespace std; // 引入命名空间string str = "hello";cout << str << endl; // "hello"cin >> str; // 输入 "hello world"cout << str << endl; // "world" 还是和长度息息相关bool __bool;// 输入number的非0值,都返回1// 输入0返回0,输入字符,字符串都返回0cin >> __bool;cout << __bool << endl;getchar();return 0;}
++a,a++,—a,a—
#include <iostream> // 预处理#include <string> // 引入这个才能使用string类型int main(){using namespace std; // 引入命名空间int a = 0;int b = ++a; // 先执行 a += 1 然后在赋值int c = a++; // 先赋值给c,在进行a += 1// --a,a--都一样cout << a << endl; // 2cout << b << endl; // 1cout << c << endl; // 1return 0;}
取余,比较运算符
取余:只允许整型的取余;
比较运算符:返回 bool 类型,值是 0 | 1;
int a = 10 > 2;bool b = 10 < 2;cout << a << endl; // 1cout << b << endl; // 0
循环
while (条件) {}; // 条件为真则,执行
do {} while (条件); // 条件为真则,执行
for (int i = 0; i < 10; i++) {}
#include <iostream> // 预处理#include <string> // 引入这个才能使用string类型int main(){using namespace std; // 引入命名空间int a = 10;while(a > 0) {cout << a << endl; // 输出 10,9,8,...a--;}a = 10;do {cout << a << endl; // 输出 10,9,8,...a--;}while(a > 0);for (int i = 0; i < 10; i++){cout << i << endl; // 输出 0,1,2,...}return 0;}
goto语句
#include <iostream> // 预处理int main(){using namespace std; // 引入命名空间goto Flag;cout << "b" << endl; // 这里会被跳过Flag:cout << "a" << endl;return 0;}
数组
#include <iostream> // 预处理#include <string> // 引入这个才能使用string类型int main(){using namespace std; // 引入命名空间int arr[5] = {1,2,3,4,5};// * 运算符 它会取得指针指向的内容// & 运算符 他会取得变量的指针int len = sizeof(arr) / sizeof(int);cout << len << endl;int *p = &arr[len - 1];for (int i = 0; i < 5; i++) {cout << *(p-i) << endl;}return 0;}
