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

Why the expression a==--a true in if statement?

#include int main() { int a = 10; if (a == a--) printf("TRUE 1\t"); a = 10; if (a == --a) printf("TRUE 2\t"); } Why is the second if statement true? Output is: …
Pankaj Mahato
  • 1,051
  • 5
  • 14
  • 26
6
votes
2 answers

Behaviour of PreIncrement and PostIncrement operator in C and Java

I'm running the following programs in Visual C++ and Java: Visual C++ void main() { int i = 1, j; j = i++ + i++ + ++i; printf("%d\n",j); } Output: 6 Java: public class Increment { public static void main(String[] args) { …
Jainendra
  • 24,713
  • 30
  • 122
  • 169
6
votes
3 answers

Can we reliably pre-increment/decrement rvalues?

For example, std::vector::iterator it = --(myVec.end());. This works in GCC 4.4 but I have heard a rumor that it's not portable.
ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80
5
votes
2 answers

Confusion about an error: lvalue required as unary '&' operand

I have been trying to understand pointer concepts by writing simple code, and I got an error problem, and it seems like I couldn't solve it or understand it. #include int *foo(void); int main(void) { printf("%d\n", *foo()); …
5
votes
3 answers

Closure Compiler - can a++ >= 3 become ++a > 3?

I admit that I asked a question about why Closure Compiler does not shorten certain code which looks shortenable at first sight a few days ago already, but that reason is not applicable in this case and I'm not really sure why it isn't shortened…
pimvdb
  • 151,816
  • 78
  • 307
  • 352
5
votes
4 answers

Why is the first assignment of str1[0]? Shouldn't it be of str1[1]?

In the for loop, we are using ++i which means that i is incremented to 1 before the loop starts to execute. What am I getting wrong here? Here is the code: #include #include int main() { char str1[100], str2[100]; int…
A.Vik
  • 93
  • 7
5
votes
3 answers

Why have for-loops with pre-increment the same behaviour as those with post-increment?

It's probably fair to say everyone learns writing for-loops using post-increment: for(var i = 0; i < 10; i++) { console.log(i); // 0..9 } When I swap the post-increment out for a pre-increment, I'd expect the following: for(var i = 0; i < 10;…
Christian
  • 6,070
  • 11
  • 53
  • 103
5
votes
3 answers

Passing increment/decrement operator to a function

I have the same function with the only difference that it will either increment or decrement. I would like to generalize from that. template void f(int& i, O op){ op(i); } int main() { int i; f(i,operator++); f(i,operator--); …
kirill_igum
  • 3,953
  • 5
  • 47
  • 73
5
votes
2 answers

Pre-Increment Operators when Using the Variable on the Same Line

I -believe- that what I'm trying to do is probably valid because it is separated in both instances by a comma (not a typical assignment), but I have no idea for sure and search isn't bringing up anything about these two specific situations. In both…
Simca
  • 65
  • 5
5
votes
1 answer

pre-increment vs post-increment - for-loop speed

I never saw a tutorial or some lecture, which showed a classic for-loop witout the post-increment-order. for (int i=0; i
codepleb
  • 10,086
  • 14
  • 69
  • 111
5
votes
7 answers

Post-increment and pre-increment operator in C

Why is k not getting incremented whereas,i and j are getting incremented in the same expression.And i also want to know what is the output of the program.I am getting the output as -2 3 1 0 #include void main() { int i=-3, j=2, m, k=0; …
Rishab777
  • 565
  • 6
  • 14
5
votes
5 answers

Why is i=i+1 faster than i++?

Test this code in Flash: var i:int = 0; for (var j:int = 0; j < 5000000; j++) { i=i+1; }// use about 300ms. i = 0; for (var j:int = 0; j < 5000000; j++) { i++; }// use about 400ms i = 0; for (var j:int = 0; j < 5000000; j++) { ++i; }//…
5
votes
3 answers

Post increment and Pre increment in C

I have a question about these two C statements: x = y++; t = *ptr++; With statement 1, the initial value of y is copied into x then y is incremented. With statement 2, We look into the value pointed at by *ptr, putting that into variable t, then…
Pkp
  • 929
  • 6
  • 19
  • 31
5
votes
1 answer

Pre-increment in PHP with magic get and set defined

I have a problem that has been spoiling the way I want to do the things for a long time. It's related to the use of magic get and set in PHP and trying to do a pre-increment over an object. I have a PHP class like the following: class Foo { …
makmonty
  • 465
  • 3
  • 12
4
votes
2 answers

Java pre and post incrementing

I am having trouble understanding the following code block. int count = 0; for (int i = 0; i < 3; i++){ count += (count++); System.out.println("count = " + count); System.out.println("i = " + i); } My understanding is…