小括号里从左往右
int x, y, z;
int a = (x = 10, y = 40, z = x + y);
cout << z << ' ' << a; //50 50
宏展开
#include <iostream>
#define MA(x) x*(x-1)
using namespace std;
void main()
{
int a = 1, b = 2;
cout << MA(1 + a + b) << endl; //8
}
变成了 1+a+b*(1+a+b)
工程里有
do{...}while(0)
C语言数组退化成指针
// 退化 成了地址 所以传进来的参数加一个长度
void f1(int arr[10])
{
cout << sizeof(arr) / sizeof(arr[0]) << endl; //1
}
int main()
{
int array1[] = { 1,2,3,4,5,6,7,8,9,10 };
cout << sizeof(array1) / sizeof(array1[0]) << endl; //10
f1(array1);
}
C++ 使用容器及引用
C语言移位
char a1 = 0x63; // 0110 0011
printf("0x%x\n", a1);
a1 >>= 4;
printf("0x%x\n", a1); // 0000 0110 逻辑右移
a1 = 0x98; // 1001 1000
a1 >>= 4;
printf("0x%x\n", a1); // 0xfffffff9 算术右移
unsigned char a2 = 0x98;// 换成unsigned
a2 >>= 4;
printf("0x%x\n", a2); // 0x9
所以只有unsigned才能右移,同时注意别移过头了
C++使用bitset
bitset<8>a3 = 0x98;
a3 >>= 4;
cout << a3 << endl;
C的强制类型转换
比较大小不要有无符号类型
unsigned int a1 = 1;
if (a1 > -1)
cout << "yes" << endl;
else
cout << "no" << endl;