#include // 定义了输入、输出流等
int main(int argc, char *argv[]) { // main 函数
std::cout << "HelloWorld" << std::endl;
return 0;
}
void Annotaion() {
// 打印 #
std::cout << "#####" << std::endl;
/*
std::cout << "#####" << std::endl;
这条语句打印 #
*/
}
#include // 头文件
int main() {
std::cout << "HelloWorld" << std::endl;
return 0;
}
namespace demo { // 命名空间
void Namespace() {
std::cout << "#####" << std::endl;
}
}
void VariableName() {
int name; // valid
int Name; // valid
int name_; // valid
int name1; // valid
int name_1; // valid
int _name; // valid
int 1name; // invalid
int 1_; // invalid
}
void DefineVariable() {
int a;
a = 10;
int b = 10;
}
int Init() {
int a = 10;
int b;
b = 10;
int c = INT_MAX;
int d = c;
int e(10);
int f{10};
int g = {10};
}
符号常量 | 表示 |
---|---|
CHAR_BIT | char的位数 |
CHAR_MAX | char的最大值 |
CHAR_MIN | char的最小值 |
SCHAR_MAX | signed char的最大值 |
SCHAR_MIN | signed char的最小值 |
UCHAR_MAX | unsigned char的最大值 |
SHRT_MAX | short的最大值 |
SHRT_MIN | short的最小值 |
USHRT_MAX | unsigned short的最大值 |
INT_MAX | int的最大值 |
INT_MIN | int的最小值 |
UINT_MAX | unsigned int的最大值 |
LONG_MAX | long的最大值 |
LONG_MIN | long的最小值 |
ULONG_MAX | unsigned long的最大值 |
LLONG_MAX | long long的最大值 |
LLONG_MIN | long long的最小值 |
ULLONG_MAX | unsigned long long的最大值 |
#include
#include // 定义了符号常量
void IntegerType() {
short a = SHRT_MAX; // SHRT_MIN
int b = INT_MAX; // INT_MIN
long c = LONG_MAX; // LONG_MIN
long long d = LONG_LONG_MAX; // LONG_LONG_MIN
std::cout << "short : " << a << std::endl;
std::cout << "int : " << b << std::endl;
std::cout << "long : " << c << std::endl;
std::cout << "long long : " << d << std::endl;
std::cout << "sizeof short : " << sizeof(short) << " byte" << std::endl;
std::cout << "sizeof int : " << sizeof(int) << " byte" << std::endl;
std::cout << "sizeof long : " << sizeof(long) << " byte" << std::endl;
std::cout << "sizeof long long : " << sizeof(long long) << " byte" << std::endl;
}
输出结果:
short : 32767
int : 2147483647
long : 2147483647
long long : 9223372036854775807
sizeof short : 2 byte
sizeof int : 4 byte
sizeof long : 4 byte
sizeof long long : 8 byte
#include
#include
void UnsignedType() {
unsigned short a = USHRT_MAX;
unsigned int b = UINT_MAX;
unsigned long c = ULONG_MAX;
unsigned long long d = ULONG_LONG_MAX;
unsigned e = UINT_MAX; // unsigned int
std::cout << "unsigned short : " << a << std::endl;
std::cout << "unsigned int : " << b << std::endl;
std::cout << "unsigned long : " << c << std::endl;
std::cout << "unsigned long long : " << d << std::endl;
std::cout << "unsigned int : " << e << std::endl;
std::cout << "sizeof unsigned short : " << sizeof(unsigned short) << " byte" << std::endl;
std::cout << "sizeof unsigned int : " << sizeof(unsigned int) << " byte" << std::endl;
std::cout << "sizeof unsigned long : " << sizeof(unsigned long) << " byte" << std::endl;
std::cout << "sizeof unsigned long long : " << sizeof(unsigned long long) << " byte" << std::endl;
std::cout << "sizeof unsigned int : " << sizeof(unsigned) << " byte" << std::endl;
}
输出结果:
unsigned short : 65535
unsigned int : 4294967295
unsigned long : 4294967295
unsigned long long : 18446744073709551615
unsigned int : 4294967295
sizeof unsigned short : 2 byte
sizeof unsigned int : 4 byte
sizeof unsigned long : 4 byte
sizeof unsigned long long : 8 byte
sizeof unsigned int : 4 byte
void MaxMin() {
short a = SHRT_MAX;
short b = SHRT_MIN;
int c = INT_MAX;
int d = INT_MIN;
// ...
std::cout << "short max: " << a << " short min: " << b << std::endl;
std::cout << "short max + 1: " << a + 1 << " short min - 1: " << b - 1 << std::endl;
std::cout << "int max: " << c << " int min: " << d << std::endl;
std::cout << "int max + 1: " << c << " int min - 1: " << d << std::endl;
unsigned long e = ULONG_MAX;
std::cout << "unsigned long max: " << e << " unsigned long min: 0 " << std::endl;
std::cout << "unsigned long max + 1: " << e + 1 << " unsigned long min - 1:" << e + 1 - 1 << std::endl;
}
输出结果:
short max: 32767 short min: -32768
short max + 1: 32768 short min - 1: -32769
int max: 2147483647 int min: -2147483648
int max + 1: 2147483647 int min - 1: -2147483648
unsigned long max: 4294967295 unsigned long min: 0
unsigned long max + 1: 0 unsigned long min - 1:4294967295
void HexOctDec() {
int a = 15656;
std::cout << "Hex: " << std::hex << a << std::endl;
std::cout << "Oct: " << std::oct << a << std::endl;
std::cout << "Dec: " << std::dec << a << std::endl;
}
输出结果:
Hex: 3d28
Oct: 36450
Dec: 15656
void ConstVarType() {
auto a = 1942;
std::cout << "int: " << (typeid (a) == typeid (int)) << std::endl;
auto b = 1942U;
std::cout << "unsigned int: " << (typeid (b) == typeid (unsigned int)) << std::endl;
auto c = 1942L;
std::cout << "long: " << (typeid (c) == typeid (long)) << std::endl;
auto d = 1942UL;
std::cout << "unsigned long: " << (typeid (d) == typeid (unsigned long)) << std::endl;
auto e = 1942LL;
std::cout << "long long: " << (typeid (e) == typeid (long long)) << std::endl;
auto f = 1942ULL;
std::cout << "unsigned long long: " << (typeid (f) == typeid (unsigned long long)) << std::endl;
auto g = static_cast(1942);
std::cout << "short: " << (typeid (g) == typeid (short)) << std::endl;
auto h = static_cast(1942);
std::cout << "unsigned short: " << (typeid (h) == typeid (unsigned short)) << std::endl;
auto i = 1942.0;
std::cout << "double: " << (typeid (i) == typeid (double)) << std::endl;
auto j = 1942.0f;
std::cout << "float: " << (typeid (j) == typeid (float)) << std::endl;
}
输出结果:
int: 1
unsigned int: 1
long: 1
unsigned long: 1
long long: 1
unsigned long long: 1
short: 1
unsigned short: 1
double: 1
float: 1
void CharType() {
std::cout <<"char max: " << CHAR_MAX << " char min: " << CHAR_MIN << std::endl;
std::cout <<"unsigned char max: " << UCHAR_MAX << " unsigned char min: " << 0 << std::endl;
char a = 'H';
char b = 'e';
char c = 'l';
char d ='l';
char e = 'o';
std::cout << a << b << c << d << e << std::endl;
char f = 56;
std::cout << "f : " << f << std::endl;
std::cout << "f : " << static_cast(f) << std::endl;
char g;
std::cout << "please input a character: ";
std::cin >> g; // 等待从终端输入字符赋值给变量g
std::cout << "print character: ";
std::cout.put(g); // 打印一个字符
std::cout << std::endl;
}
输出结果:
char max: 127 char min: -128
unsigned char max: 255 unsigned char min: 0
Hello
f : 8
f : 56
please input a character: H
print character: H
字符名称 | ASCII符号 | c++代码 | 十进制ASCII码 | 十六进制ASCII码 |
---|---|---|---|---|
换行符 | NL(LF) | \n | 10 | 0XA |
水平制表符 | HT | \t | 9 | 0X9 |
垂直制表符 | VT | \v | 11 | 0XB |
退格 | BS | \b | 8 | 0X8 |
回车 | CR | \r | 13 | 0XD |
振铃 | BEL | \a | 7 | 0X7 |
反斜杠 | \ | \\ | 92 | 0X5C |
问号 | ? | ? | 63 | 0X3F |
单引号 | ’ | ’ | 39 | 0X27 |
双引号 | " | " | 34 | 0X22 |
void BoolType() {
bool a = true;
bool b = false;
bool c = 10;
bool d = 0;
int e = 10;
bool f = e;
int g = f;
std::cout << "a : " << a << std::endl;
std::cout << "b : " << b << std::endl;
std::cout << "c : " << c << std::endl;
std::cout << "d : " << d << std::endl;
std::cout << "e : " << e << std::endl;
std::cout << "f : " << f << std::endl;
std::cout << "g : " << g << std::endl;
}
输出结果:
a : 1
b : 0
c : 1
d : 0
e : 10
f : 1
g : 1
void FloatType() {
float a = 1.0;
double b = 1E-4;
long double c = 1.0;
std::cout << "a: " << a << "\tb: " << b << "\tc: " << c << std::endl;
std::cout << "sizeof float: " << sizeof (float) << std::endl;
std::cout << "sizeof double: " << sizeof (double) << std::endl;
std::cout << "sizeof long double: " << sizeof (long double) << std::endl;
std::cout << "double max: " << __DBL_MAX__ << "\tdouble min: " << __DBL_MIN__ << std::endl;
}
输出结果:
a: 1 b: 0.0001 c: 1
sizeof float: 4
sizeof double: 8
sizeof long double: 16
double max: 1.79769e+308 double min: 2.22507e-308
void ConvertType() {
int a = 10;
long long b = a;
double c = 4.5;
int d = c;
int *e = &a;
long *f = reinterpret_cast(e); // 强制类型转换
std::cout << "a: " << a << std::endl;
std::cout << "b: " << b << std::endl;
std::cout << "c: " << c << std::endl;
std::cout << "d: " << d << std::endl;
std::cout << "*e: " << *e << std::endl;
std::cout << "*f: " << *f << std::endl;
}
输出结果:
a: 10
b: 10
c: 4.5
d: 4
*e: 10
*f: 10
void ArrayType() { // 一维数组
int arr[10]; // 声明数组未初始化,值是随机值
for (int i = 0; i < 10; ++i) // 循环打印数组元素
std::cout << arr[i] << " ";
std::cout << std::endl;
arr[0] = 10; // 局部元素赋值,索引访问数组元素
arr[2] = 20;
for (int i = 0; i < 10; ++i) // 循环打印数组元素
std::cout << arr[i] << " ";
std::cout << std::endl;
for (int i = 0; i < 10; ++i) // 数组每个元素赋值
arr[i] = i;
for (int i = 0; i < 10; ++i) // 循环打印数组元素
std::cout << arr[i] << " ";
std::cout << std::endl;
int *p = arr; // 指针指向数组,数组名就是数组的首地址指针
for (int i = 0; i < 10; ++i) // 循环打印数组元素
std::cout << p[i] << " ";
std::cout << std::endl;
// 数组初始化方式
int a[4] = {1}; // 初始化第一个元素,其余元素默认为 0
for (int i = 0; i < 4; ++i) // 循环打印数组元素
std::cout << a[i] << " ";
std::cout << std::endl;
int b[2] = {1, 2}; // 初始化所有元素
for (int i = 0; i < 2; ++i) // 循环打印数组元素
std::cout << b[i] << " ";
std::cout << std::endl;
int c[] = {1, 2, 3}; // 全部元素初始化并自动推导数组元素个数
for (int i = 0; i < 3; ++i) // 循环打印数组元素
std::cout << c[i] << " ";
std::cout << std::endl;
int d[2]{0, 2}; // c++11的初始化列表
for (int i = 0; i < 2; ++i) // 循环打印数组元素
std::cout << d[i] << " ";
std::cout << std::endl;
int e[10] = {}; // 所有元素初始化为零
for (int i = 0; i < 10; ++i) // 循环打印数组元素
std::cout << e[i] << " ";
std::cout << std::endl;
int f[10]{}; // 所有元素初始化为零
for (int i = 0; i < 10; ++i) // 循环打印数组元素
std::cout << f[i] << " ";
std::cout << std::endl;
}
输出结果:
6684064 0 14755456 0 6684240 0 16 0 8 0
10 0 20 0 6684240 0 16 0 8 0
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
1 0 0 0
1 2
1 2 3
0 2
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0