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
11
votes
5 answers

How exactly does the ?: operator work in C?

I have a question, how the compiler operate on the following code: #include int main(void) { int b=12, c=11; int d = (b == c++) ? (c+1) : (c-1); printf("d = %i\n", d); } I am not sure why the result is ‍‍‍d = 11.
J0S
  • 145
  • 7
11
votes
3 answers

C# Post Increment

While I am testing post increment operator in a simple console application, I realized that I did not understand full concept. It seems weird to me: int i = 0; bool b = i++ == i; Console.WriteLine(b); The output has been false. I have expected that…
Umut Derbentoğlu
  • 1,146
  • 5
  • 18
  • 39
11
votes
2 answers

Is `x-- > 0 && array[x]` well-defined behavior in C++?

Can I use x on both sides of a boolean expression when I post-increment it on the left side? The line in question is: if(x-- > 0 && array[x]) { /* … use x … */ } Is that defined through the standard? Will array[x] use the new value of x or the old…
knittl
  • 246,190
  • 53
  • 318
  • 364
11
votes
1 answer

Why isn't mySet.erase(it++) undefined behavior, or is it?

Accordint to this quite highly upvoted answer, the canonical way to iterate through a set erasing some elements is the following: for (it = mySet.begin(); it != mySet.end(); ) { if (conditionToDelete(*it)) { mySet.erase(it++); } …
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
10
votes
3 answers

Why post-increment needs to make a copy while pre-increment does not

I know this issue has been discussed several times , but I could not find a post which explains why a copy needs to be made in case of a post-increment operation. Quoting from a stackoverflow reply: int j = i++; // j will contain i, i will be…
ralzaul
  • 4,280
  • 6
  • 32
  • 51
10
votes
6 answers

What are the historical reasons C languages have pre-increments and post-increments?

(Note: I am not asking about the definitions of pre-increment vs. post-increment, or how they are used in C/C++. Therefore, I do not think this is a duplicate question.) Developers of C (Dennis Ritchie et al) created increment and decrement…
ShanZhengYang
  • 16,511
  • 49
  • 132
  • 234
10
votes
4 answers

i++ vs. ++i in a JavaScript for loop

Because of JSLint, I almost always use i += 1 to increment a JavaScript for loop, but for quick and dirty scripts, I use i++ instead. However, I see a lot of for loops in other people's code in which they increment i by doing ++i instead. As far as…
HartleySan
  • 7,404
  • 14
  • 66
  • 119
10
votes
4 answers

Understanding more about i++ and i=i+1

I was wondering if there is difference between the two forms of increment. Some of the links says i++ is faster that i=i+1; Also as one of the person my observation is also the same for assembly code. please check the image where both the assembly…
Sagrian
  • 1,048
  • 2
  • 11
  • 29
10
votes
3 answers

Post-increment x by n (n != 1)

So, if I execute the following code... int x = 0; Debug.WriteLine(x++); Debug.WriteLine(x += 4); Debug.WriteLine(x); ... I get 0, 5, and 5, respectively. What I'd like to get, however is 0, 1, and 5. Is there any way to do a post-increment by n in…
user502255
9
votes
8 answers

C/C++ Post-increment by more than one

I'm reading bytes from a buffer. But sometimes what I'm reading is a word or longer. // assume buffer is of type unsigned char * read_ptr(buffer+(position++)) That's fine but how can I post-increment position by 2 or 4? There's no way I can get…
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
9
votes
4 answers

Post-Increment Operator: Unexpected Behavior

Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…) My code is as follows: #include int main() { int x = 10, y = 0; x = x++; printf("x: %d\n", x); y = x++; printf("y: %d\n",…
danben
  • 80,905
  • 18
  • 123
  • 145
9
votes
4 answers

Order of operations for pre-increment and post-increment in a function argument?

I have some C code: main() { int a=1; void xyz(int,int); xyz(++a,a++); //which Unary Operator is executed first, ++a or a++? printf("%d",a); } void xyz(int x,int y) { printf("\n%d %d",x,y); } The function xyz has two…
thulasi
  • 109
  • 1
  • 1
  • 5
9
votes
2 answers

Puzzling behaviour of == after postincrementation

Someone postulated in some forum thread that many people and even experienced Java Developers wouldn't understand the following peace of Java Code. Integer i1 = 127; Integer i2 = 127; System.out.println(i1++ == i2++); System.out.println(i1 ==…
Aufwind
  • 25,310
  • 38
  • 109
  • 154
9
votes
3 answers

What should be the output of echo ++$a + $a++

In the PHP manual, operator precedence section, there is this example: // mixing ++ and + produces undefined behavior $a = 1; echo ++$a + $a++; // may print 4 or 5 I understand the behavior is undefined because of the following reason: Since x + y…
Salman A
  • 262,204
  • 82
  • 430
  • 521
9
votes
6 answers

a += a++ * a++ * a++ in Java. How does it get evaluated?

I came across this problem in this website, and tried it in Eclipse but couldn't understand how exactly they are evaluated. int x = 3, y = 7, z = 4; x += x++ * x++ * x++; // gives x = 63 System.out.println(x); y = y * y++; …
Ashok Bijoy Debnath
  • 1,493
  • 1
  • 17
  • 27