C语言中的增量后和增量前

C语言中的增量后和增量前,c,post-increment,pre-increment,operator-precedence,C,Post Increment,Pre Increment,Operator Precedence,我对这两个C语句有一个问题: x=y++ t=*ptr++ 对于语句1,y的初始值被复制到x中,然后y递增 在语句2中,我们查看*ptr指向的值,将其放入变量t中,然后在稍后增加ptr 对于语句1,后缀增量运算符的优先级高于赋值运算符。那么,y不应该先递增,然后将x分配给y的递增值吗 在这些情况下,我不理解运算符优先级。您对2]的含义理解错误。增量后总是产生增量之前的值,然后在增量后的某个时间增加该值 因此,t=*ptr++本质上等同于: t = *ptr; ptr = ptr + 1; 这同

我对这两个C语句有一个问题:

  • x=y++

  • t=*ptr++

  • 对于语句1,y的初始值被复制到x中,然后y递增

    在语句2中,我们查看*ptr指向的值,将其放入变量t中,然后在稍后增加ptr

    对于语句1,后缀增量运算符的优先级高于赋值运算符。那么,y不应该先递增,然后将x分配给y的递增值吗


    在这些情况下,我不理解运算符优先级。

    您对
    2]
    的含义理解错误。增量后总是产生增量之前的值,然后在增量后的某个时间增加该值

    因此,
    t=*ptr++
    本质上等同于:

    t = *ptr;
    ptr = ptr + 1;
    
    这同样适用于您的
    1]
    ——从
    y++
    生成的值是增量之前的
    y
    的值。优先级不会改变这一点——不管表达式中其他运算符的优先级有多高或多低,它产生的值始终是增量之前的值,增量将在之后某个时间进行。

    C中增量前和增量后的差异: 增量前和增量后是内置的。一元的意思是:“有一个输入的函数”。“运算符”表示:“对变量进行修改”

    增量(++)和减量(-)内置一元运算符修改它们附加到的变量。如果试图对常量或文字使用这些一元运算符,则会出现错误

    在C中,以下是所有内置一元运算符的列表:

    Increment:         ++x, x++
    Decrement:         −−x, x−−
    Address:           &x
    Indirection:       *x
    Positive:          +x
    Negative:          −x
    Ones_complement:  ~x
    Logical_negation:  !x
    Sizeof:            sizeof x, sizeof(type-name)
    Cast:              (type-name) cast-expression
    
    这些内置运算符是伪装的函数,它们接受变量输入并将计算结果放回同一变量中

    后增量示例:

    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = y++;       //variable x receives the value of y which is 5, then y 
                   //is incremented to 6.
    
    //Now x has the value 5 and y has the value 6.
    //the ++ to the right of the variable means do the increment after the statement
    
    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = ++y;       //variable y is incremented to 6, then variable x receives 
                   //the value of y which is 6.
    
    //Now x has the value 6 and y has the value 6.
    //the ++ to the left of the variable means do the increment before the statement
    
    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = y--;       //variable x receives the value of y which is 5, then y 
                   //is decremented to 4.
    
    //Now x has the value 5 and y has the value 4.
    //the -- to the right of the variable means do the decrement after the statement
    
    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = --y;       //variable y is decremented to 4, then variable x receives 
                   //the value of y which is 4.
    
    //x has the value 4 and y has the value 4.
    //the -- to the right of the variable means do the decrement before the statement
    
    预增量示例:

    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = y++;       //variable x receives the value of y which is 5, then y 
                   //is incremented to 6.
    
    //Now x has the value 5 and y has the value 6.
    //the ++ to the right of the variable means do the increment after the statement
    
    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = ++y;       //variable y is incremented to 6, then variable x receives 
                   //the value of y which is 6.
    
    //Now x has the value 6 and y has the value 6.
    //the ++ to the left of the variable means do the increment before the statement
    
    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = y--;       //variable x receives the value of y which is 5, then y 
                   //is decremented to 4.
    
    //Now x has the value 5 and y has the value 4.
    //the -- to the right of the variable means do the decrement after the statement
    
    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = --y;       //variable y is decremented to 4, then variable x receives 
                   //the value of y which is 4.
    
    //x has the value 4 and y has the value 4.
    //the -- to the right of the variable means do the decrement before the statement
    
    后减量示例:

    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = y++;       //variable x receives the value of y which is 5, then y 
                   //is incremented to 6.
    
    //Now x has the value 5 and y has the value 6.
    //the ++ to the right of the variable means do the increment after the statement
    
    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = ++y;       //variable y is incremented to 6, then variable x receives 
                   //the value of y which is 6.
    
    //Now x has the value 6 and y has the value 6.
    //the ++ to the left of the variable means do the increment before the statement
    
    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = y--;       //variable x receives the value of y which is 5, then y 
                   //is decremented to 4.
    
    //Now x has the value 5 and y has the value 4.
    //the -- to the right of the variable means do the decrement after the statement
    
    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = --y;       //variable y is decremented to 4, then variable x receives 
                   //the value of y which is 4.
    
    //x has the value 4 and y has the value 4.
    //the -- to the right of the variable means do the decrement before the statement
    
    预减量示例:

    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = y++;       //variable x receives the value of y which is 5, then y 
                   //is incremented to 6.
    
    //Now x has the value 5 and y has the value 6.
    //the ++ to the right of the variable means do the increment after the statement
    
    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = ++y;       //variable y is incremented to 6, then variable x receives 
                   //the value of y which is 6.
    
    //Now x has the value 6 and y has the value 6.
    //the ++ to the left of the variable means do the increment before the statement
    
    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = y--;       //variable x receives the value of y which is 5, then y 
                   //is decremented to 4.
    
    //Now x has the value 5 and y has the value 4.
    //the -- to the right of the variable means do the decrement after the statement
    
    int x = 0;     //variable x receives the value 0.
    int y = 5;     //variable y receives the value 5
    
    x = --y;       //variable y is decremented to 4, then variable x receives 
                   //the value of y which is 4.
    
    //x has the value 4 and y has the value 4.
    //the -- to the right of the variable means do the decrement before the statement
    

    如果这让你感到困惑,你可能会想读一读,把它说出来。正如我所说,指针首先递增。检查以下内容:“后缀++运算符的结果是操作数的值。获得结果后,操作数的值将递增。”(来自C99标准的§6.5.2.4/2)。您是正确的。我想我可以相信c-faq,遵循标准it是最好的策略。道歉。@Pkp:我认为你误解了常见问题。这里要解决的问题是
    ++
    是否将应用于指针本身,或者指针所指的内容(顺便说一句,这也是优先级在本例中确定的内容)。答案是它适用于指针本身,而不是指针对象。