Questions tagged [post-increment]

For issues relating to defining or performing post increment operations.

Post-increment operators increase the value of their operand by 1 (or another specified amount), but the value of the operand in the expression is the operand's original value prior to the increment operation.

561 questions
17
votes
5 answers

Java: pre-,postfix operator precedences

I have two similar questions about operator precedences in Java. First one: int X = 10; System.out.println(X++ * ++X * X++); //it prints 1440 According to Oracle tutorial: postfix (expr++, expr--) operators have higher precedence than prefix…
D.R.
  • 175
  • 2
  • 9
17
votes
3 answers

Is ++x more efficient than x++ in Java?

During a programming class, the professor was teaching us about x++ and ++x, with x being an integer. He said that in the scenario we are able to just put either x++ or ++x, ++x is more efficient (by little, but still, in theory, more efficient…
Saturn
  • 17,888
  • 49
  • 145
  • 271
16
votes
2 answers

Is *p++ += 2 well defined?

I'm not sure whether statement below is well defined by standard C or not *p1++ += 2; or other similar statement: *E1++ = E2 From standard C about post-increment: The result of the postfix ++ operator is the value of the operand. …
dragon135
  • 1,366
  • 8
  • 19
15
votes
10 answers

Post increment operator behavior

Possible Duplicate: Pre & post increment operator behavior in C, C++, Java, & C# Here is a test case: void foo(int i, int j) { printf("%d %d", i, j); } ... test = 0; foo(test++, test); I would expect to get a "0 1" output, but I get "0…
Benoit
  • 37,894
  • 24
  • 81
  • 116
15
votes
20 answers

What is more efficient, i++ or ++i?

Exact Duplicate: Is there a performance difference between i++ and ++i in C++? Exact Duplicate: Difference between i++ and ++i in a loop? What is more efficient, i++ or ++i? I have only used this in Java and C/C++, but I am really asking for all…
Berek Bryan
  • 13,755
  • 10
  • 32
  • 43
14
votes
3 answers

Why is "++i++" invalid while (++i)++ is valid?

Let's consider the following code: int main() { int i = 2; int b = ++i++; return 3; } It compiles with the following with an error: : In function 'int main()': :3:16: error: lvalue required as increment operand 3 |…
Bktero
  • 722
  • 5
  • 15
14
votes
6 answers

Increment operator inside array

I have a C program which does queue operations using an array. In that program, they increment a variable inside array. I can't understand how that works. So, please explain these operations: array[++i]; array[i++];
Loki San Hitleson
  • 177
  • 1
  • 2
  • 7
14
votes
7 answers

Array increment operator in C

I don't understand the results of following code: #include #include int main() { int a[4]={1, 3, 5, 6}; //suppose a is stored at location 2010 printf("%d\n", a + 2); printf("%d", a++); return 0; } Why does the…
user221458
  • 716
  • 2
  • 7
  • 17
14
votes
5 answers

Why does post-increment work on wrapper classes

I was doing a review of some code and came across an instance of someone post-incrementing a member variable that was a wrapper class around Integer. I tried it myself and was genuinely surprised that it works. Integer x = 0; System.out.print(x++…
Jherico
  • 28,584
  • 8
  • 61
  • 87
13
votes
3 answers

post increment operator java

I can't make heads or tails of the following code from "java puzzlers" by joshua bloch. public class Test22{ public static void main(String args[]){ int j=0; for(int i=0;i<100;i++){ j=j++; } System.out.println(j); //prints 0 int…
srandpersonia
  • 131
  • 1
  • 4
13
votes
2 answers

Is there a performance difference between i++ and ++i in JavaScript?

I read Is there a performance difference between i++ and ++i in C?: Is there a performance difference between i++ and ++i if the resulting value is not used? What's the answer for JavaScript? For example, which of the following is better? for…
Oriol
  • 274,082
  • 63
  • 437
  • 513
12
votes
6 answers

Operator precedence in the given expressions

Expression 1: *p++; where p is a pointer to integer. p will be incremented first and then the value to which it is pointing to is taken due to associativity(right to left). Is it right? Expression 2: a=*p++; where p is a pointer to integer. Value of…
Vipul Prakash
  • 173
  • 1
  • 9
12
votes
5 answers

How is *it++ valid for output iterators?

In example code, I often see code such as *it++ for output iterators. The expression *it++ makes a copy of it, increments it, and then returns the copy which is finally dereferenced. As I understand it, making a copy of an output iterator…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
12
votes
10 answers

C programming ++ operator

Why does this code always produce x=2? unsigned int x = 0; x++ || x++ || x++ || x++ || ........; printf("%d\n",x);
user397232
  • 1,750
  • 6
  • 21
  • 30
11
votes
1 answer

Will the inefficiency of the postfix ++/-- operators be optimised away for STL iterators?

I know that the postfix versions of the increment/decrement operators will generally be optimised by the compiler for built-in types (i.e. no copy will be made), but is this the case for iterators? They are essentially just overloaded operators, and…
Dominic Gurto
  • 4,025
  • 2
  • 18
  • 16
1 2
3
37 38