Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In C++, the integer variable bar evaluates to 1 in this code

int foo = 0;
int bar = ++foo;

and 0 in this code

int foo = 0;
int bar = foo++;

Is this behavior prevalent in most popular languages?

share|improve this question
4  
Thats exactly to specification in languages with that operator. Have you looked at what those operators mean? –  Rig Mar 25 '12 at 16:39
add comment

1 Answer

up vote 10 down vote accepted

The C-like languages, such as C++, Java, and C# all have this behavior.

The definitely not C-like languages such as ruby or python typically don't have an increment or decrement operator whatsoever.

So many popular languages do have this behavior, but other popular languages don't have those operators at all.

share|improve this answer
1  
Python doesn't have ++, but it does have +=. –  dan04 Mar 25 '12 at 5:09
    
@dan04, absolutely. I don't why you brought it up. –  Winston Ewert Mar 25 '12 at 14:48
    
What about languages like Perl, which aren't C-like, but have ++ and -- –  Dynamic Mar 25 '12 at 21:29
    
@Jae, I believe Perl is like the C-like languages in this regard. –  Winston Ewert Mar 27 '12 at 0:48
    
An interesting difference between Java and C/C++ is that result of more complex expressions including pre- and post-increment operators (such as x = x++ + ++x - x--) tend to be undefined/implementation-defined in C/C++ and are strictly defined in Java. That's a source of much "why doesn't this (insanely complex) code work in Java" questions. I don't know exactly, but I guess C# went the Java route on this one. –  Joachim Sauer Mar 28 '12 at 7:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.