Pkp*_*Pkp 0 c operator-precedence pre-increment post-increment
我对这两个C语句有疑问:
x = y++;
t = *ptr++;
使用语句1,y的初始值被复制到x然后y递增.
使用语句2,我们查看*ptr指向的值,将其放入变量t,然后稍后增加ptr.
对于语句1,后缀增量运算符的优先级高于赋值运算符.所以不应该首先递增y,然后将x赋值给y的递增值?
在这些情况下,我不理解运算符优先级.
预自增和后自增是内置的一元运算符。一元表示:“具有一个输入的函数”。“运算符”的意思是:“对变量进行修改”。
\n\n自增 (++) 和自减 (--) 内置一元运算符修改它们所附加的变量。如果您尝试对常量或文字使用这些一元运算符,您将收到错误。
\n\n在 C 中,以下是所有内置一元运算符的列表:
\n\nIncrement: ++x, x++\nDecrement: \xe2\x88\x92\xe2\x88\x92x, x\xe2\x88\x92\xe2\x88\x92\nAddress: &x\nIndirection: *x\nPositive: +x\nNegative: \xe2\x88\x92x\nOnes_complement: ~x\nLogical_negation: !x\nSizeof: sizeof x, sizeof(type-name)\nCast: (type-name) cast-expression\nRun Code Online (Sandbox Code Playgroud)\n\n这些内置运算符是伪装的函数,它们接受变量输入并将计算结果放回到同一变量中。
\n\n后置增量示例:
\n\nint x = 0; //variable x receives the value 0.\nint y = 5; //variable y receives the value 5\n\nx = y++; //variable x receives the value of y which is 5, then y \n //is incremented to 6.\n\n//Now x has the value 5 and y has the value 6.\n//the ++ to the right of the variable means do the increment after the statement\nRun Code Online (Sandbox Code Playgroud)\n\n预增量示例:
\n\nint x = 0; //variable x receives the value 0.\nint y = 5; //variable y receives the value 5\n\nx = ++y; //variable y is incremented to 6, then variable x receives \n //the value of y which is 6.\n\n//Now x has the value 6 and y has the value 6.\n//the ++ to the left of the variable means do the increment before the statement\nRun Code Online (Sandbox Code Playgroud)\n\n后减量示例:
\n\nint x = 0; //variable x receives the value 0.\nint y = 5; //variable y receives the value 5\n\nx = y--; //variable x receives the value of y which is 5, then y \n //is decremented to 4.\n\n//Now x has the value 5 and y has the value 4.\n//the -- to the right of the variable means do the decrement after the statement\nRun Code Online (Sandbox Code Playgroud)\n\n预减示例:
\n\nint x = 0; //variable x receives the value 0.\nint y = 5; //variable y receives the value 5\n\nx = --y; //variable y is decremented to 4, then variable x receives \n //the value of y which is 4.\n\n//x has the value 4 and y has the value 4.\n//the -- to the right of the variable means do the decrement before the statement\nRun Code Online (Sandbox Code Playgroud)\n
你错了你的意思2].后递增总是从递增之前产生值,然后在某个时间之后递增该值.
因此,t = *ptr++基本上相当于:
t = *ptr;
ptr = ptr + 1;
Run Code Online (Sandbox Code Playgroud)
这同样适用于您1]- 产生y++的值是y增量前的值.优先级不会改变 - 无论表达式中其他运算符的优先级有多高或多少,它产生的值将始终是增量之前的值,并且增量将在之后的某个时间完成.
| 归档时间: |
|
| 查看次数: |
6484 次 |
| 最近记录: |