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
3
votes
1 answer

Evaluation of Expressions inside printf in C

I know that multiple expressions are evaluated from right to left. Ex: int i = 0; printf("%d %d %d", i, i++, i++); // Prints 2 1 0 But when it comes to each expression to be evaluated, i'm not getting if it is from right to left or vice…
Nir_J
  • 133
  • 1
  • 3
  • 7
3
votes
1 answer

Postfix vs. prefix increment with lambda expressions

This question is definitely not a duplicate because I do not refer to general post/precrement evaluation but specifically to the use of increment in lambda expressions, which is provably different. Everyone is free to confirm that the code will…
AdHominem
  • 1,204
  • 3
  • 13
  • 32
3
votes
3 answers

Pre/Post Increment Pointers in C++

*(p1++) int array[10] = {1,2}; int *p1 = array; *p1=24; *p1= *(p1++); for (int i : array) cout << i << " "; Output is 24 24 *(++p1) int array[10] = {1,2}; int *p1 = array; *p1=24; *p1= *(++p1); for (int i : array) cout << i << " "; Output…
Patrick Duncan
  • 549
  • 1
  • 6
  • 17
3
votes
1 answer

logical operation & pre-increment in c

Can any one explain why c still equal 15 after execution int main(void) { int t,a=5,b=10,c=15; t= ++a||++c; printf("%d %d %d",t,a,c); }
yassin
  • 121
  • 8
3
votes
3 answers

Increment and logical operators precedence

In the program shown below, prefix should be evaluated first because it has higher precedence, But answer is -2, 2, 0, 1 and it is explained in book "as LHS of || is true RHS is not evaluated." Why is it so? All the increments should performed first…
codeluv
  • 305
  • 1
  • 15
3
votes
3 answers

Different Output for b=+1 in java

I executed the below program int b = 0; b=+1; System.out.println(b); b=+1; System.out.println(b); b=+1; System.out.println(b); and got output like 1 always. Why is the value of b incrementing in the first increment and…
robin
  • 1,893
  • 1
  • 18
  • 38
3
votes
3 answers

whether a language needs preIncrement (++x) and postIncrement (x++)

I have never seen the usecase for pre-increment and post-increment in actual code. The only place i see them most often are puzzles. My opinion is, it introduces more confusion rather than being useful. is there any real use case scenario for…
3
votes
2 answers

Why does `--var` and `var-1` work differently?

I did write a code to print all valid combinations of n-pair parentheses. However, at my first try, the algorithm output all the combinations twice, that is, . The code was: public static void solve(char[] string, int open, int closed, int…
Ayberk
  • 410
  • 10
  • 21
3
votes
4 answers

Confusing output after use of increment operator

#include main() { int a[5] = {5,1,15,20,25}; int i,j,m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d %d %d\n",i,j,m); } Okay now the program compiles and runs fine. But the output which i get is 3 2 15 i.e i…
3
votes
1 answer

Is there a difference between ++i and i++ in this loop?

The array.prototype.reduce function at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce It has the following loop: for (index = 0; length > index; ++index) { if (this.hasOwnProperty(index)) { …
ciso
  • 2,887
  • 6
  • 33
  • 58
3
votes
1 answer

pre-increment not working as i expect

I am trying to learn dynamic programming by solving some questions online. One question that i came across requires the to process the following input 4 10 3 4 4 5 6 7 5 7 The first points at number of items, second the total capacity and the rest…
Arun Khetarpal
  • 197
  • 1
  • 3
  • 10
3
votes
3 answers

K&R seems to prefer pre-increment

I'm working through K&R and am presently on Exercise 1-16. It occurs to me that thus far only pre-increment has been used in the book. Most other tutorial books and indeed source code I've seen tend to favour post-increment, except where there is an…
retrodev
  • 2,323
  • 6
  • 24
  • 48
3
votes
3 answers

Prefix operator difference in C++ and C#

Let's have this piece of code: int a = 1; int b = ++a + ++a; In C++ (VS 2010) the result is: b = 6 but in C# the result is: b = 5 What's going on there? Why are the results different?
PavelS
  • 260
  • 1
  • 8
3
votes
7 answers

Semantics of pre- and postfix "++" operator in Java

I wondering to know why this snippet of code give output 112 How this last digit 2 was creating? public static void main(String[] args) { int i = 0; System.out.print(++i); System.out.print(i++); System.out.print(i); Why does this…
user2374612
  • 115
  • 1
  • 4
3
votes
2 answers

Post-Incrementing/decrementing in recursive method calls (Java)

Say you have a recursive method, and you post-increment/decrement a value in the recursive call. Why will this result in a stack overflow exception when a pre-increment/decrement will not? Ex. numberCount(currentNumber++); //Stack overflow…
Derrek Whistle
  • 701
  • 2
  • 7
  • 16