What is the difference between ++i and i++
来源:互联网 发布:apache curator下载 编辑:程序博客网 时间:2024/06/16 03:42
Refer from http://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i
In C, what is the difference between using ++i
and i++
, and which should be used in the incrementation block of afor
loop?
up vote418 down vote accepted
++i
will increment the value ofi
, and then return the incremented value.i = 1; j = ++i; (i is 2, j is 2)
i++
will increment the value ofi
, but return the original value thati
held before being incremented.i = 1; j = i++; (i is 2, j is 1)
For a for
loop, either works. ++i
seems more common, perhaps because that is what is used in K&R.
In any case, follow the guideline "prefer ++i
over i++
" and you won't go wrong.
There's a couple of comments regarding the efficiency of ++i
and i++
. In any non-student-project compiler, there will be no performance difference. You can verify this by looking at the generated code, which will be identical.
The efficiency question is interesting... here's my attempt at an answer:Is there a performance difference between i++ and ++i in C?
As On Freund notes, it's different for a C++ object, since operator++()
is a function and the compiler can't know to optimize away the creation of a temporary object to hold the intermediate value.
i++ is known as Post Increment whereas ++i is called Pre Increment.
i++
i++ is post increment because it increments i's value by 1 after the operation is over.
Lets see the following example:
int i = 1, j;
j = i++;
Here value of j = 1 but i = 2. Here value of i will be assigned to j first then i will be incremented.
++i
++i is pre increment because it increments i's value by 1 before the operation.It means j = i; will execute after i++.
Lets see the following example:
int i = 1, j;
j = ++i;
Here value of j = 2 but i = 2. Here value of i will be assigned to j after the i incremention of i.Similarly ++i will be executed before j=i;.
For your question which should be used in the incrementation block of a for loop? the answer is, you can use any one.. no matter. It will execute your for loop same no. of times.
for(i=0; i<5; i++)
printf("%d ",i);
And
for(i=0; i<5; ++i)
printf("%d ",i);
Both the loops will produce same output. ie 0 1 2 3 4.
It only matters where you are using it.
for(i = 0; i<5;)
printf("%d ",++i);
In this case output will be 1 2 3 4 5.
- What is the difference between ++i and i++
- What is the difference between -I and -L in makefile? and the means of -d.
- What is the difference between XHTML MP, XHTML Basic, WML, i-mode, and HDML?
- What is the difference between test, [ and [[ ?
- what is the difference between the setTimeout and setInterval
- What is the difference between DHTML and the DOM?
- What is the difference between the mouseover and mouseenter events?
- What is the difference between AWT and SWT?
- What is the difference between User Control and Custom Control?
- What is the difference between an EXE and a DLL?
- What is the difference between XML and C or C ?
- What is the difference between SMOD and CMOD?
- what is the difference between little-endian and big-endian.
- What is the difference between procedural and nonprocedural language?
- What is the difference between a thread and a process?
- What is the difference between objectForKey and valueForKey?
- What is the difference between Microsoft TMG and UAG ?
- What is the Difference Between http and https?
- hdu 1231 最大连续子序列
- 详细介绍Java垃圾回收机制
- java学习(8)java内存分配管理
- MFC中 fstream常见用法
- mysql数据库 实现类似标记序号的伪列
- What is the difference between ++i and i++
- SVM十问十答
- Android 性能优化之使用MAT分析内存泄露问题
- Java正则表达式(二)
- C++ 4种类型类型转换
- 关于子函数返回字符串问题集锦
- GCD
- ACM 油田合并
- Android App 内存泄露之Handler