全部博文(136)
分类: C/C++
2011-03-16 14:12:12
C provides two unusual operators for incrementing and decrementing variables. The increment operator ++ adds 1 to its operand, while decrement operator – subtracts 1.
The unusual aspect is that ++ and – may be used either as prefix operators, or postfix operators. In both cases, the effect is to increment n. But the expression ++n increments n before its value is used, while n++ increments n after its value has been used.
The increment and decrement operators can only be applied to variables; an expression like (i+l)++ is illegal.
In a context where no value is wanted, just the incrementing effect, as in
prefix and postfix are the same. But there are situations where one or the other is specifically called for. For instance, consider the function squeeze(s,c), which removes all occurrences of the character c from the string s.
Each time a non-*c* occurs, it is copied into the current j position, and only then is j incremented to be ready for the next character. This is exactly equivalent to
Date: 2011-03-16 14:07:41 EDT
HTML generated by org-mode 7.4 in emacs 23