在C语言中,使用++i和i++之间的区别是什么,在for循环的增量块中应该使用哪个?
当前回答
A =i++表示A包含当前i的值。
A =++i表示A包含i的增量值。
其他回答
A =i++表示A包含当前i的值。
A =++i表示A包含i的增量值。
在循环中使用两者的有效结果是相同的。换句话说,循环将在两个实例中做完全相同的事情。
就效率而言,选择i++而不是++i可能会有一定的损失。根据语言规范,使用后增量操作符应该创建操作符所作用的值的额外副本。这可能是额外操作的来源。
但是,您应该考虑上述逻辑的两个主要问题。
Modern compilers are great. All good compilers are smart enough to realize that it is seeing an integer increment in a for-loop, and it will optimize both methods to the same efficient code. If using post-increment over pre-increment actually causes your program to have a slower running time, then you are using a terrible compiler. In terms of operational time-complexity, the two methods (even if a copy is actually being performed) are equivalent. The number of instructions being performed inside of the loop should dominate the number of operations in the increment operation significantly. Therefore, in any loop of significant size, the penalty of the increment method will be massively overshadowed by the execution of the loop body. In other words, you are much better off worrying about optimizing the code in the loop rather than the increment.
在我看来,整个问题可以归结为一种风格偏好。如果您认为预增量更具可读性,那么就使用它。就我个人而言,我更喜欢后增量,但这可能是因为这是我在了解优化之前学到的东西。
这是一个典型的过早优化的例子,像这样的问题有可能分散我们对设计中严肃问题的注意力。然而,这仍然是一个很好的问题,因为在“最佳实践”中没有统一的用法或共识。
i++和++ I
这段小代码可能有助于从不同的角度来看待与已经发布的答案的差异:
int i = 10, j = 10;
printf ("i is %i \n", i);
printf ("i++ is %i \n", i++);
printf ("i is %i \n\n", i);
printf ("j is %i \n", j);
printf ("++j is %i \n", ++j);
printf ("j is %i \n", j);
结果是:
//Remember that the values are i = 10, and j = 10
i is 10
i++ is 10 //Assigns (print out), then increments
i is 11
j is 10
++j is 11 //Increments, then assigns (print out)
j is 11
注意之前和之后的情况。
for循环
至于在for循环的增量块中应该使用它们中的哪一个,我认为我们能做的最好的决定是使用一个很好的例子:
int i, j;
for (i = 0; i <= 3; i++)
printf (" > iteration #%i", i);
printf ("\n");
for (j = 0; j <= 3; ++j)
printf (" > iteration #%i", j);
结果是:
> iteration #0 > iteration #1 > iteration #2 > iteration #3
> iteration #0 > iteration #1 > iteration #2 > iteration #3
我不知道你怎么想,但我没有看到它的用法有任何不同,至少在for循环中是这样。
:不久
++i和i++的工作原理是一样的,如果你不在函数中写它们的话。如果你使用function(i++)或function(++i),你可以看到区别。
函数(++i)表示首先将i增加1,然后将i放入具有新值的函数中。
Function (i++)表示在i加1之后将第一个i放入函数中。
int i=4;
printf("%d\n",pow(++i,2));//it prints 25 and i is 5 now
i=4;
printf("%d",pow(i++,2));//it prints 16 i is 5 now
这种差异可以通过下面这段简单的c++代码来理解:
int i, j, k, l;
i = 1; //initialize int i with 1
j = i+1; //add 1 with i and set that as the value of j. i is still 1
k = i++; //k gets the current value of i, after that i is incremented. So here i is 2, but k is 1
l = ++i; // i is incremented first and then returned. So the value of i is 3 and so does l.
cout << i << ' ' << j << ' ' << k << ' '<< l << endl;
return 0;
推荐文章
- errno线程安全吗?
- 如何在C程序中获取当前目录?
- 如何在shell中为for循环添加前导零?
- 互斥实例/教程?
- 如何添加一个'或'条件在#ifdef
- extern关键字对C函数的影响
- 如果使用if-return-return或if-else-return?
- 转换Python程序到C/ c++代码?
- 为什么程序不是经常用汇编编写的?
- 有没有替换Windows (Visual C)的unistd.h ?
- 如何在for循环中注释类型?
- 使用gcc命令行从.c文件构建.so文件
- C多行宏:do/while(0) vs作用域块
- time_t最终的类型定义是什么?
- 我需要显式处理负数或零时,总和平方数字?