在C语言中,使用++i和i++之间的区别是什么,在for循环的增量块中应该使用哪个?
当前回答
主要的区别是 i++ Post(After Increment)和 ++i Pre(增量前) Post if I =1,循环增量为1,2,3,4,n 如果I =1,循环会增加2 3 4 5 n
其他回答
这种差异可以通过下面这段简单的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;
预填充是指在同一行上的增量。后增量是指行执行后的增量。
int j = 0;
System.out.println(j); // 0
System.out.println(j++); // 0. post-increment. It means after this line executes j increments.
int k = 0;
System.out.println(k); // 0
System.out.println(++k); // 1. pre increment. It means it increments first and then the line executes
当它带有OR和AND操作符时,它变得更有趣。
int m = 0;
if((m == 0 || m++ == 0) && (m++ == 1)) { // False
// In the OR condition, if the first line is already true
// then the compiler doesn't check the rest. It is a
// technique of compiler optimization
System.out.println("post-increment " + m);
}
int n = 0;
if((n == 0 || n++ == 0) && (++n == 1)) { // True
System.out.println("pre-increment " + n); // 1
}
在数组中
System.out.println("In Array");
int[] a = { 55, 11, 15, 20, 25 };
int ii, jj, kk = 1, mm;
ii = ++a[1]; // ii = 12. a[1] = a[1] + 1
System.out.println(a[1]); // 12
jj = a[1]++; // 12
System.out.println(a[1]); // a[1] = 13
mm = a[1]; // 13
System.out.printf("\n%d %d %d\n", ii, jj, mm); // 12, 12, 13
for (int val: a) {
System.out.print(" " + val); // 55, 13, 15, 20, 25
}
在c++中,指针变量的后/前增量
#include <iostream>
using namespace std;
int main() {
int x = 10;
int* p = &x;
std::cout << "address = " << p <<"\n"; // Prints the address of x
std::cout << "address = " << p <<"\n"; // Prints (the address of x) + sizeof(int)
std::cout << "address = " << &x <<"\n"; // Prints the address of x
std::cout << "address = " << ++&x << "\n"; // Error. The reference can't reassign, because it is fixed (immutable).
}
下面的C代码片段说明了前后递增和递减操作符之间的区别:
int i;
int j;
增量运算符:
i = 1;
j = ++i; // i is now 2, j is also 2
j = i++; // i is now 3, j is 2
++i将增加i的值,然后返回增加后的值。 I = 1; J = ++i; (i = 2, j = 2) i++将增加I的值,但返回I被增加前的原始值。 I = 1; J = i++; (i = 2, j = 1)
对于For循环,两者都可以。++i似乎更常见,也许是因为这是K&R中使用的。
在任何情况下,遵循“优先选择++i而不是i++”的指导方针,您都不会出错。
这里有一些关于++i和i++的效率的评论。在任何非学生项目的编译器中,都不会有性能差异。您可以通过查看生成的代码来验证这一点,它们是相同的。
效率问题很有趣……以下是我试图回答的问题: 在C语言中i++和++i之间有性能差异吗?
正如@OnFreund指出的那样,对于c++对象是不同的,因为操作符++()是一个函数,编译器不知道优化掉一个临时对象的创建来保存中间值。
i++被称为后增量,而++ I被称为前增量。
i++
i++是后增量,因为它在操作结束后将I的值加1。
让我们看看下面的例子:
int i = 1, j;
j = i++;
这里j = 1,但i = 2。在这里,i的值将首先赋给j,然后i将增加。
++i
++i是预增量,因为它在操作之前将i的值加1。 它表示j = i;将在i++之后执行。
让我们看看下面的例子:
int i = 1, j;
j = ++i;
这里j = 2但是i = 2。这里i的值将在i增加i之后赋给j。 类似地,++i将在j=i;之前执行。
对于你的问题,在For循环的增量块中应该使用哪个?答案是,你可以用任何一个…没关系。它将执行相同次数的for循环。
for(i=0; i<5; i++)
printf("%d ", i);
And
for(i=0; i<5; ++i)
printf("%d ", i);
两个循环将产生相同的输出。也就是0 1 2 3 4。
重要的是你在哪里使用它。
for(i = 0; i<5;)
printf("%d ", ++i);
在这种情况下,输出将是1 2 3 4 5。