简体   繁体   English

为什么分配时X的值不增加

[英]Why the value of X is not incremented while assignment

I'm new to C language and I'm not entirely sure why on Line 4 when post-increment is done the value of x isn't change what I mean is that 我是C语言的新手,我不太确定为什么在第4行上完成后增量后x的值不会改变,我的意思是

x = printf("%d",x++);

The value for x was 12 so the printf would print 12 and then x should be assigned 2 and while ++ was there x should be later changed with 2+1 and on line 6 pre-increment is done so output shouldn't be 124 . x的值为12因此printf将打印12 ,然后应将x分配为2 ,而在++ ,应稍后用2+1更改x ,并在第6行进行预增量,因此输出不应为124

Why x on line 4 isn't added? 为什么没有在第4行添加x?

Please help. 请帮忙。

#include <stdio.h>

int main(){

int x = 12;

x = printf("%d", x++);

printf("%d", ++x);

return 0;

}

Make yourself aware of sequence point . 使自己意识到序列点 From this [emphasis mine] : 这个 [强调我的]

There is a sequence point after the evaluation of all function arguments and of the function designator, and before the actual function call . 在所有函数自变量和函数指示符的求值之后,以及实际函数调用之前,有一个顺序点。

From this [emphasis mine] : 这个 [强调我的]

Increment operators initiate the side-effect of adding the value 1 of appropriate type to the operand. 增量运算符会引发将适当类型的值1添加到操作数的副作用。 Decrement operators initiate the side-effect of subtracting the value 1 of appropriate type from the operand. 减量运算符会产生从操作数中减去适当类型的值1的副作用。 As with any other side-effects, these operations complete at or before the next sequence point . 与其他副作用一样,这些操作将在下一个序列点或之前完成

Looks at this statement: 查看以下语句:

x = printf("%d", x++);

The post increment operator increase the value of operand by 1 but the value of the expression is the operand's original value prior to the increment operation . 后递增运算符将操作数的值加1但是表达式的值是该操作数在增量运算之前的原始值
So, the value of x passed to printf() will be its original value which is 12 and due to sequence point, before calling printf() the value of x will be incremented by 1 . 因此,传递给printf()x的值将是其原始值12并且由于序列点,在调用printf()之前, x的值将增加1 The return value of printf() will be assigned to x which overwrites the last value of x which is the incremented value due to post ++ operator. printf()的返回值将分配给x ,它将覆盖x的最后一个值, x的最后一个值是由于post ++运算符而增加的值。 Hence, after this statement the value of x is 2 . 因此,在此语句之后, x值为2

The value for x was 12 so the printf would print 12 and then x should be assigned 2 and while ++ was there x should be later changed with 2+1 and on line 6 pre-increment is done so output shouldn't be 124. x的值为12,因此printf将打印12,然后应将x分配为2,而在++处,应稍后用2 + 1更改x,并在第6行进行预增量,因此输出不应为124 。

no, the assignment is done after all concerning printf("%d", ++x); 不,分配是在所有涉及printf("%d", ++x); , your code is equivalent to that : ,您的代码等效于:

#include <stdio.h>

int main(){
  int x = 12;

  int y = printf("%d", x++);

  x = y;

  printf("%d", ++x);

  return 0;
}

so x = printf("%d", ++x); 所以x = printf("%d", ++x); does : 会:

  • printf writes 12 printf写12
  • then x is incremented to value 13 然后x递增到值13
  • then x is assigned to the result of printf valuing 2 然后x被赋值给printf值 2

then you execute printf("%d", ++x); 然后执行printf("%d", ++x); while x values 2 before, so x is incremented before to be given in argument, so 3 is written x的值在2之前,因此x在参数中给定之前先增加,所以3被写入

and the final print result is 123 最终的打印结果是123

PS. PS。 As said by @HS in an other remark :There is a sequence point after the evaluation of all function arguments (x++ is an argument to printf()) and the pre/post increment/decrement operation complete at or before the next sequence point. 正如@HS在另一句话中所说:在所有函数参数的求值之后(x ++是printf()的参数),存在一个序列点,并且在下一个序列点或之前完成前/后递增/递减操作。

This will work for you: 这将为您工作:

++x is prefix increment and the x++ is postfix increment, and here is the difference on both of them: ++ x是前缀增量, x ++是后缀增量,这是两者的区别:

The prefix increment returns the value of a variable after it has been incremented. 变量增加后,前缀增量返回变量的值。

On the other hand, the more commonly used postfix increment returns the value of a variable before it has been incremented. 另一方面,更常用的后缀增量在变量增加之前返回变量的值。

#include <stdio.h>

int main()
{
    int x = 12;

    x = printf("%d", ++x);

    printf("%d", ++x);

    return 0;
}

x= printf("%d", x++); x = printf(“%d”,x ++); This is about the size of that integer. 这大约是该整数的大小。 x++ is in printf it is printing the value of x and increase the value by one. x ++在printf中,它正在打印x的值并将该值增加一。 But it doesn't affect the size of that integer. 但这不会影响该整数的大小。 Then you assign the size of x to x not the value. 然后,将x的大小分配给x而不是值。 That's why that incremental had not worked there. 这就是为什么增量文件在那里不起作用的原因。

I think you can understand what I am saying. 我想你能理解我的意思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM