C语言各个符号优先级(全)

  • Post author:
  • Post category:其他


常见符号测试代码如下:

void priority_test1() {
	int a = 2, b = 2, c;
	//-- 与正负
	c = -++a;
	cout << c << endl;
	cout << endl;
	//正负与乘除
	a = 10;
	c = -++a % 4;
	cout << c << endl;
	c = -++a * 2;
	cout << c << endl;
	cout << endl;
	//*/%
	a = 10, b = 4;
	c = a % b * b / 3;
	cout << c << endl;

	//+-与赋值
	a = 10, b = 4, c = 2;
	c -= a - b;
	cout << c << endl;

	a = 10, b = 4, c = 2;
	c *= (-++a % c * c - b / 2) * 5;
	cout << c << endl;
}


结论

偏僻符号测试如下

	//位或(逻辑)小于乘除 位移>|
	int a = 8, b = 2, c;
	c = a << 4 % 3;
	cout << c << endl;
	cout << endl;

	a = 8, b = 2;
	c = a << 4 | 0b11;
	cout << c << endl;
	cout << endl;

	a = 8, b = 2;
	c = a | 3 << 2;  // 11*4先与
	cout << c << endl;
	c = a | 12;
	cout << c << endl;
	cout << endl;

	//位移与加减
	c = 2 + 1 << 3;
	cout << c << endl;

	//位移与关系逻辑
	c =   1 & 5 << 2;
	cout << "c:" << c << endl;
	b = 5 << 2;
	cout << (2 & b)  << endl;
	cout << c << endl;

	c =  1 << 2 + 1;
	cout << "c:" << c << endl;
	c =  1 | 1 << 2 + 1;
	cout << "c:" << c << endl;
	c =  1 | 1 << 2 | 2;//100==-101--111
	cout << "c:" << c << endl;

所遇全部符号优先级

	a = !0 + 5 && 2;
	cout  << a << endl;
	a = !0 + 5 << 2;
	cout  << a << endl;



版权声明:本文为qq_43519774原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。