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
2
votes
3 answers

Difference between pre and post decrement in recursive function argument

I have following sample code where i used pre-decrement void function(int c) { if(c == 0) { return; } else { cout << "DP" << c << endl; function(--c); cout << c << endl; return; } } This function give output for…
DigviJay Patil
  • 986
  • 14
  • 31
2
votes
1 answer

C++: operator overloading: in-class and out-class. Ambiguity with preincrement operator

Check it out this code: struct A { A operator+(A const& a) { cout << 1 << endl; return A(); } A& operator++() { cout << 2 << endl; return *this; } A operator++(int) { cout << 3 << endl; return *this; } bool operator!() { cout << 4 <<…
ABu
  • 10,423
  • 6
  • 52
  • 103
2
votes
3 answers

The difference between n++ and ++n at the end of a while loop? (ANSI C)

this is probably a dumb question but I just can't figure it out. It has to do with the differences between n++ and ++n (which I thought I understood but apparently not). #include #include long algorithmA(int n); long…
Steady
  • 33
  • 1
  • 1
  • 6
2
votes
4 answers

Prefix and postfix increment in for loop

Given the following code int j = 0; for (int i = 0; i < str.Length; ++i) { if (i==j) { Console.WriteLine ("equal"); } j++; } I expected that ++i would change i from initial 0 to 1 and thus i==j evaluated to false. But it did…
TOP KEK
  • 2,593
  • 5
  • 36
  • 62
2
votes
1 answer

bash: unexpected array member handle

I found weird the following bash behaviour in the last command line. For me, it is totally unexpected. $ set | grep ^BASH_VERSINFO # Show the array BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release"…
Jdamian
  • 3,015
  • 2
  • 17
  • 22
2
votes
3 answers

Unpredicted language behavior i++

I tried to do the following i=0; if (i++ % Max_Col_items == 0 && i !=0) { } and discovered that it increased i in the middle i % Max_Col_items == 0; i=i+1; i !=0; when I though it would add increase i in the end: i % Max_Col_items == 0; i…
Nahum
  • 6,959
  • 12
  • 48
  • 69
2
votes
10 answers

Pre increment and post increment

I'm having trouble understanding how Post Increment (++), Pre Increment (--) and addition/subtraction work together in an example. x++ means add 1 to the variable. x-- means subtract 1 from the variable. But I am confused with this example: int x =…
Hoon
  • 1,571
  • 5
  • 15
  • 19
2
votes
6 answers

Why java statement evaluation is happening like these ?

int z = 1; System.out.println(z++ == ++z); System.out.println(++z == z++); the output will be: false true and I don't get why, please explain this to me.
user1611561
2
votes
4 answers

pointer increment and dereference (lvalue required error)

I am trying to understand how pointer incrementing and dereferencing go together, and I did this to try it out: #include int main(int argc, char *argv[]) { char *words[] = {"word1","word2"}; printf("%p\n",words); …
yasar
  • 13,158
  • 28
  • 95
  • 160
1
vote
10 answers

Increment, preincrement and postincrement

Help me to resolve this please. The steps that follows that expressions are: //Expression offSpring1[m1++] = temp1; //Steps: 1.- increment m1 2.- assign temp1 to offSpring I have always thought that the expression inside the brackets was the…
Guido
  • 39
  • 4
1
vote
5 answers

how does increment work?

Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…) Undefined Behavior and Sequence Points Ok we all know that i++ increments value by 1 on next line and ++i increments on same line(please correct me…
mOj
  • 27
  • 5
1
vote
1 answer

C++ evaluation order and possible outcome

Consider following code: #include int main() { int i = 0; std::cout << ++i << ' ' << --i << std::endl; } In his "C++17 The Complete Guide" book Nicolai Josuttis writes that before C++17 this particular example might produce…
Mati
  • 753
  • 4
  • 20
1
vote
1 answer

Why does "cout << ++Object;" produce an error? (Both << and ++ have been overloaded)

In the code below, both << and ++ operators have been overloaded: #include using namespace std; class Test{ public: int num=0; Test operator++(){ num++; return *this; } }; ostream &operator<<(ostream &mystream,…
1
vote
4 answers

difference between ++i and i++ in for loop

I understand well how postfix and prefix increments/decrements work. But my question is, in a for loop, which is more efficient or faster, and which is more commonly used and why? Prefix? for(i = 0; i < 3; ++i) {...} Or postfix? for(i = 0; i < 3;…
Neigyl R. Noval
  • 6,018
  • 4
  • 27
  • 45
1
vote
0 answers

How is the output of this expression 43?

I am learning C language and now I am confused with the output of the below snippet. #include int main(void) { int p = 20; printf("%d\n",++p + p++); return 0; } Online Fiddle How is the output of this 43? As far as I have…
varun_2799
  • 19
  • 2