如果這個工具幫到了你,可以請作者喝杯咖啡 ☕
線上檢視C語言運算子優先順序與結合性,助你理解表示式求值。
a++tools.leve-c-query.categories.postfixtools.leve-c-query.associativity.left先返回当前值,然后将变量加 1。
i++a--tools.leve-c-query.categories.postfixtools.leve-c-query.associativity.left先返回当前值,然后将变量减 1。
i--a()tools.leve-c-query.categories.postfixtools.leve-c-query.associativity.left调用函数并传递参数。
printf("hi")a[]tools.leve-c-query.categories.postfixtools.leve-c-query.associativity.left访问数组的元素,等价于 *(a+i)。
arr[0].tools.leve-c-query.categories.postfixtools.leve-c-query.associativity.left访问结构体或联合体的成员。
obj.name->tools.leve-c-query.categories.postfixtools.leve-c-query.associativity.left通过指针访问结构体或联合体成员,等价于 (*p).x。
p->next(type){list}tools.leve-c-query.categories.postfixtools.leve-c-query.associativity.leftC99 起在表达式中创建一个匿名对象并初始化。
(int[]){1,2,3}++atools.leve-c-query.categories.unarytools.leve-c-query.associativity.right先将变量加 1,再返回新值。
++i--atools.leve-c-query.categories.unarytools.leve-c-query.associativity.right先将变量减 1,再返回新值。
--i+atools.leve-c-query.categories.unarytools.leve-c-query.associativity.right返回操作数的值,触发整型提升。
+x-atools.leve-c-query.categories.unarytools.leve-c-query.associativity.right返回操作数的相反数。
-x!tools.leve-c-query.categories.unarytools.leve-c-query.associativity.right对操作数取反,0 变 1,非 0 变 0。
!ready~tools.leve-c-query.categories.unarytools.leve-c-query.associativity.right对整数按位取反,0 变 1,1 变 0。
~mask(type)tools.leve-c-query.categories.unarytools.leve-c-query.associativity.right将表达式显式转换为目标类型。
(int)3.14*atools.leve-c-query.categories.unarytools.leve-c-query.associativity.right读取指针所指向地址处的值。
*p&atools.leve-c-query.categories.unarytools.leve-c-query.associativity.right获取变量在内存中的地址。
&valuesizeoftools.leve-c-query.categories.unarytools.leve-c-query.associativity.right返回类型或表达式占用的字节数,编译期求值。
sizeof(int)_Alignoftools.leve-c-query.categories.unarytools.leve-c-query.associativity.rightC11 起返回类型的对齐字节数。
_Alignof(double)*tools.leve-c-query.categories.multiplicativetools.leve-c-query.associativity.left两个操作数相乘。
a * b/tools.leve-c-query.categories.multiplicativetools.leve-c-query.associativity.left整数除法向零截断;浮点除法保留小数。
10 / 3%tools.leve-c-query.categories.multiplicativetools.leve-c-query.associativity.left返回两个整数相除的余数,操作数必须为整型。
10 % 3+tools.leve-c-query.categories.additivetools.leve-c-query.associativity.left两数相加,或指针与整数相加得到偏移地址。
a + b-tools.leve-c-query.categories.additivetools.leve-c-query.associativity.left两数相减,或两个指针相减得到偏移元素个数。
a - b<<tools.leve-c-query.categories.shifttools.leve-c-query.associativity.left整数按位左移,相当于乘以 2 的幂。
1 << 3>>tools.leve-c-query.categories.shifttools.leve-c-query.associativity.left整数按位右移,有符号数实现定义、无符号数为逻辑右移。
8 >> 2<tools.leve-c-query.categories.relationaltools.leve-c-query.associativity.left比较两个操作数,左小于右返回 1,否则返回 0。
a < b<=tools.leve-c-query.categories.relationaltools.leve-c-query.associativity.left左小于或等于右返回 1,否则返回 0。
a <= b>tools.leve-c-query.categories.relationaltools.leve-c-query.associativity.left左大于右返回 1,否则返回 0。
a > b>=tools.leve-c-query.categories.relationaltools.leve-c-query.associativity.left左大于或等于右返回 1,否则返回 0。
a >= b==tools.leve-c-query.categories.equalitytools.leve-c-query.associativity.left判断两个值是否相等。
a == b!=tools.leve-c-query.categories.equalitytools.leve-c-query.associativity.left判断两个值是否不相等。
a != b&tools.leve-c-query.categories.bitwisetools.leve-c-query.associativity.left两个整数按位执行逻辑与,两位都为 1 时结果为 1。
a & b^tools.leve-c-query.categories.bitwisetools.leve-c-query.associativity.left两个整数按位执行异或,相同为 0、不同为 1。
a ^ b|tools.leve-c-query.categories.bitwisetools.leve-c-query.associativity.left两个整数按位执行逻辑或,任一位为 1 结果为 1。
a | b&&tools.leve-c-query.categories.logicaltools.leve-c-query.associativity.left短路逻辑与:左操作数为 0 时不计算右操作数。
a && b||tools.leve-c-query.categories.logicaltools.leve-c-query.associativity.left短路逻辑或:左操作数为非 0 时不计算右操作数。
a || b?:tools.leve-c-query.categories.conditionaltools.leve-c-query.associativity.right根据条件返回两个表达式之一。
x > 0 ? 1 : -1=tools.leve-c-query.categories.assignmenttools.leve-c-query.associativity.right将右操作数的值赋给左操作数。
x = 10+=tools.leve-c-query.categories.assignmenttools.leve-c-query.associativity.rightx = x + y 的简写。
x += 1-=tools.leve-c-query.categories.assignmenttools.leve-c-query.associativity.rightx = x - y 的简写。
x -= 1*=tools.leve-c-query.categories.assignmenttools.leve-c-query.associativity.rightx = x * y 的简写。
x *= 2/=tools.leve-c-query.categories.assignmenttools.leve-c-query.associativity.rightx = x / y 的简写。
x /= 2%=tools.leve-c-query.categories.assignmenttools.leve-c-query.associativity.rightx = x % y 的简写。
x %= 3<<=tools.leve-c-query.categories.assignmenttools.leve-c-query.associativity.rightx = x << y 的简写。
x <<= 2>>=tools.leve-c-query.categories.assignmenttools.leve-c-query.associativity.rightx = x >> y 的简写。
x >>= 2&=tools.leve-c-query.categories.assignmenttools.leve-c-query.associativity.rightx = x & y 的简写。
x &= 0xFF^=tools.leve-c-query.categories.assignmenttools.leve-c-query.associativity.rightx = x ^ y 的简写。
x ^= mask|=tools.leve-c-query.categories.assignmenttools.leve-c-query.associativity.rightx = x | y 的简写。
x |= 0x01,tools.leve-c-query.categories.commatools.leve-c-query.associativity.left依次求值左、右操作数,整个表达式的结果为右操作数的值,常用于 for 语句步进。
for (i=0,j=9; i<j; ++i,--j)在編寫或閱讀C語言程式碼時,經常因運算子優先順序和結合性不熟悉而導致表示式求值錯誤。本工具提供C語言全部運算子的優先順序順序和結合方向一覽表,並支援互動式查詢,幫助你快速確認任意運算子的優先順序高低和結合性,從而準確理解或編寫複雜表示式。
本工具展示的優先順序表基於標準C語言(C89/C99/C11),請留意不同特定編譯器擴充套件運算子可能未包含在內。查詢結果僅供參考,建議結合程式碼邏輯進行驗證。工具為純前端執行,不會上傳任何輸入內容,保護您的隱私。如在表格中未找到需要的運算子,可能是該運算子不被標準C支援或屬於前處理器指令。
熟練掌握運算子優先順序能減少不必要的括號,提升程式碼可讀性和執行效率。但考慮到可維護性,在可能產生歧義的表示式中使用括號明確意圖是良好實踐。例如,表示式 (a & mask) == b 中按位與的優先順序低於關係運算符,需加括號確保先進行位運算。建議定期參考本工具進行復習或作為編碼時快速檢查的輔助。