Hi all,
Can any one tell the difference between the following.
a++
a+=1
Both will do the same operation.I want to know that any one of these have a advantage than the other.
Hi all,
Can any one tell the difference between the following.
a++
a+=1
Both will do the same operation.I want to know that any one of these have a advantage than the other.
While ++a and a++ mean the same thing when they form statements independently, they behave differently when they are used in expressions on the RHS of an assignment statement.
For eg.
a=5;
y=++a;
In this case the value of y would be 6. But in this case
a=5;
y=a++;
here y would be 5 but a would be 6
On the other hand a+=1 is shorthand form of a=a+1.
Last edited by BEN10; 07-07-2009 at 06:47 AM.
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
Thank you BEN10.But already I know what you explained.I want to know whether both of these statement have some difference in the case of speed or not.
They are two different kinds of operators. ++ is a unary operator (whether x++ or ++x). += is an assignment operator. I doubt there is much difference in speed but I don't know. The potential use value is slightly different:
Code:x += y++;
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I'd say that it depends on compiler optimisations.
I'm guessing that ++x would be translated by most compilers into the assembly 'INC' (increment) instruction mnemonic, which may or may not be faster than 'ADD'.
If a compiler optimises any code with a 'x + 1' to the increment instruction then there would be no difference.
Same goes for decrementing.
Note that this is just speculation. I don't know if any of this is what actually happens.
Assignment operators evaluate the l-value only once, which is an important thing to watch out for if it contains any side effects.
Eg.
array[i++] += 1;
will do the right thing.
Either of these attempts leads to all sorts of trouble.
array[i++] = array[i] + 1;
array[i++] = array[i++] + 1;
Doing it correctly the long way is hard work.
array[i] = array[i] + 1;
i = i + 1;
There would certainly be a big performance difference if you had
array[someFuncReturningIndex()] += 1;
because the function would only be called once.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
@Salem: good use of fonts
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Just an FYI,
I read about the origin of "C", and how the compiler favored the reigning hardware platform of that time - PDP family of minis. So the post-increments and similar had a reason for being part of the language. There were native machine instructions of that ilk: access-then-increment. I haven't encountered any micro processors which had similar instructions but then again, the desired effect can be achieved with two instructions at most.
Both VAX (32-bit version of PDP-11 really) and 68000 have post-increment and pre-decrement versions of it's instruction set. I'm fairly sure that ARM's instructions also matches the C language fairly well with it's STR/LDR instructions when setting the P and W bits to one of the instruction.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thank you! I stand corrected. I forgot about Motorolas. Never could wrap my head around 6800, 68000 addressing, registers, but probably the assembly syntax turned me off most of all. I'm a Zilog/Intel guy myself.
It doesn't? How do you set it to increment by something else?x++ doesn't mean that the increment value is 1, it can be anything
>> x++ doesn't mean that the increment value is 1, it can be anything, so the use of something like "INC" is a very special case.
Well, the only other case is in pointer arithmetic, where the increment means "advance sizeof(type) bytes from this offset". Besides that, though, I can't think of any other possible forms.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Back to the original question... even in the case of pointer arithmetic a++ and a+=1 would still be equivalent. If a compiler generates markedly different machine code which varies in execution times, then shame on it!
Yes Sebastiani, I was referring to pointer arithmetic which I think it is important to mention, since the ++ operator comes from the PDP 11 autoincrement mode : (Rn)+ (see Department of Computer Science - PDP-11 - Don S. Bidulock).
Often the question asked is "which is faster, x++ or x += 1 ?". In that case it's important to remember that "1" can be any other value (since an incrementer != adder).