Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - Confusing foo++ and ++foo

Node:Confusing foo++ and ++foo, Next:, Previous:Confusion of = and ==, Up:Run-time errors



Confusing foo++ and ++foo

In many cases, the forms foo++ and ++foo are identical. However, if they are hidden inside another statement, there can be a subtle difference. For example:

my_array [++my_index] = 0;

The code ++my_index cause my_index to be incremented by 1 before the assignment takes place, whereas my_index++ would have cause my_index to be incremented after the assignment takes place. Sometimes you'll want one and sometimes the other. If you find that your program is miscalculating by a difference of 1 (this is called an off-by-one bug and is quite common), a prefix or postfix ++ could be the cause. The same holds for other prefix and postfix operators, such as --.

 
 
  Published under free license. Design by Interspire