c++ - Necessity of Increment/Decrement Operator in C -
my doubt usage of increment/decrement
operators (++
, --
) in c
(also in c++
). absolutely necessary use ++
, --
in programs? example consider code given below,
int x=10; x++;
this can replaced follows,
int x=10; x=x+1;
so necessary use ++
, --
in practical programming? asking particular reason. in opinion 1 of confusing operators in c
, forming expressions *x++
, ++*x
, x++ + ++x
etc. , causing thousands of errors each day.
of course know direct mapping between ++
, assembly instruction incr
possible. believe decent compiler ability optimize can replace x=x+1
incr x
.
so in short question "is there situation x=x+1 can not replace x++ ?".
it might helpful if can provide piece of code not work in absence of ++
or --
.
any suggestions? thank you.
is there situation
x = x + 1
can not replacex++
?
sure, foo(x = x + 1)
different foo(x++)
because x = x + 1
yields new value of x
, x++
yields old value of x
.
beyond there's isn't difference , strictly ++
, --
aren't needed (i.e. can write programs x += 1
instead of ++x
, x -= 1
instead of --x
).
note in c++ operator++
, operator--
can overloaded , in particular useful when manipulating iterators.
Comments
Post a Comment