Questions tagged [pre-increment]

For issues relating to defining or performing pre increment operations.

Pre-increment operators increase the value of their operand by 1 (or another specified amount), and the value of the operand in the expression is the operand's value after the increment operation.

342 questions
0
votes
3 answers

Finding value of 'i' in a for loop?

I have this question here: What is i after the following for loop? The given code is: int y= 0; for (int i= 0; i<10; ++i) { y+= i; } I put that the answer is 9, but that is incorrect according to the grader. I even printed 'i' and it…
ecain
  • 1,282
  • 5
  • 23
  • 54
0
votes
1 answer

In C, is { a[i] = a[++i] } equivalent to { a[i] = a[i+1]; i++;}?

In C, is a[i] = a[++i]; equivalent to a[i] = a[i+1]; i++; That is, which side of the assignment is evaluated first and what value of i is used on the left side? Or is this assignment ambiguous?
push2eject
  • 203
  • 2
  • 6
0
votes
0 answers

Pre-increment faster than post-increment in Swift?

Which is faster in Swift? Pre-increment ++i, or post-increment i++? Is it the same as in C++? Preincrement faster than postincrement in C++ - true? If yes, why is it?
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
0
votes
2 answers

Pre and post on Java. Not taking effect?

int x = 12; int y = 15; while (y >= 0) { x = x--; y = --y; } System.out.print(x); This prints out 12 and I'm guessing x is never changed because it's stored before the post (x--) takes effect,…
Oscar F
  • 323
  • 2
  • 9
  • 21
0
votes
1 answer

is this right in C? (forbidden operation on a variable compiler error)

I am not really that advanced in C but I found this in a piece of code ssize_t ret = 0; ret += sprintf(buf + --ret, "\n"); What does this mean? And can we really do --ret in this case?
0
votes
0 answers

Weird results with post incrementing

Ok, I thought i knew about post-incrementing and pre-incrementing but... Funny /Weird /How? /Why? #include int main() { int a = 0; printf(" A++ - %d \n A++ - %d \n A++ - %d \n A++ - %d", a++, a++, a++, a++); } Without compiling…
Mugurel
  • 1,829
  • 3
  • 17
  • 26
0
votes
1 answer

behavior of --it and it-- in

What is the explanation for the behavior of it++ and ++it in the context of a function call--and more specifically, for the function iter_swap? It confuses me that call 2 and call 4 in the code below appear to swap the elements, whereas call 3 and…
Brian
  • 3,453
  • 2
  • 27
  • 39
0
votes
1 answer

Are there any side-effects in using `x += 1` rather than `++x` in C#?

I've always written for loops in C# using for (int i = 0; i < 10; i++). I've been reading up on the best-practices for JavaScript (JavaScript: The Good Parts), and one of them is to prefer x += 1 over x++. Are there any differences, in C#, between…
Steve Dunn
  • 21,044
  • 11
  • 62
  • 87
0
votes
1 answer

Pre Increment & Post Increment

Today I revisited Pre Increment and Post Increment. Basic definitions I know. Pre Increment - Increments the Value and returns the value. Post Increment - Increments the Value and returns the value prior to increment. But Doing some combinations of…
Shaurya Chaudhuri
  • 3,772
  • 6
  • 28
  • 58
0
votes
3 answers

A program with for loop

I was writing a for loop program when this code came across my mind. for(int i=1; i<=10; i++,i++) The program works fine and the output is also correct. But then I tried the following code: for(int i=1; i<=10; ++i,++i) for(int i=1; i<=10;…
shryesh.k
  • 51
  • 1
  • 7
0
votes
2 answers

Is using i++ (or ++i) really a good practice?

I know the differences between i++ and ++i (like this) if I want to use their new values. And I saw many examples in The C Programming Language (K&R) use expressions like s[i++] = c;. Sometimes, I come across codes (shown following) make me confused…
Tony
  • 5,972
  • 2
  • 39
  • 58
0
votes
1 answer

In what cases, if any, is there a difference in performance between prefix and postfix increment/decrement operators in C?

I understand that this issue in C++ can be a matter of copying the object and|or overload. I also understand that optimization comes into play and can make this a non issue in most, if not all cases. My question is whether or not current compilers…
griotspeak
  • 13,022
  • 13
  • 43
  • 54
0
votes
4 answers

Pre-increment operation in C

I'm just starting a beginner's guide to games programming tutorial in C. I'm a little confused with the code below. At main gameOverTick is set to zero, then we have a case when the game is over case GAME_OVER: printStatus("GAME OVER!!! The…
DigiSweep
  • 51
  • 1
  • 2
  • 7
0
votes
4 answers

Undefined Behavior of Postfix or Prefix Increment in Function Calls in C

I have seen in this site that prefix increment or postfix increment in a function call may cause undefined behavior. I have gone through one of those recently. The source code is something like this : #include void call(int,int,int); int…
Ishrak
  • 509
  • 1
  • 9
  • 17
0
votes
3 answers

Combined preincrement and postincrement in C

This is an ugly code used only to terrorize job applicants during interviews... But I cannot understand the logic behind it. Can someone explain why the expression with "b" is not equal to the one with "a"? #include void main(){ int a…