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

Out put of this program .Confused?

So I am stuck with this question. int i=5,a; a=++i + i++ + ++i + i++ - --i; printf("%d",a); According to me 'a' should be 20. a=6+6+8-8 However, on execution I found the answer to be 18. What I am doing wrong? A step by step explanation would be…
-3
votes
5 answers

Post Increment in do while loop if condition is TRUE (BEST WAY?)

It is quite simple, indeed. int main() { int n,i,j; n = 20; i = 0; char ch[8]; do { ch[i] = (n%2) + '0'; n /= 2; // SIMPLE WAY if(n != 0) i++; } while(n != 0); …
anon
-3
votes
2 answers

Pre and post increment in java

I know how pre and post increment operators work, but recently I found out a strange behavior in Java. What i knew so far was (as explained in this answer as well): a = 5; i=++a + ++a + a++; => i=6 + 7 + 7; (a=8) which clearly shows that the ++a…
-3
votes
2 answers

Why do pointers behave differently when operated by post increment oparator?

Let's see the first code: The following code displays the value of n=10: #include int main() { int n=10; int*p=&n; *p++; std::cout< int…
-3
votes
4 answers

Difference between switch and while increment and evaluate

If i is equal to 1, after this statement, while (i++ <= 10){} i is taken as 2 i.e., incremented before evaluation in the block. But if used in switch, switch(i++){} i gets evaluated before incremented in the block. Why these cases i++ behave…
Lyrk
  • 1,936
  • 4
  • 26
  • 48
-3
votes
3 answers

Post increment in for loop in java

int num=0 for(int i=0;i<5;i++) { num = num++; system.out.println(num); } I am little confused with the output of above program. It prints five times 0 in output. Why isn't the num variable incremented in the loop?
-3
votes
1 answer

'In any case, follow the guideline "prefer ++i over i++" and you won't go wrong.' What is the reason behind this in C?

I had come across this answer to this question. In one of the line, the author mention's: In any case, follow the guideline "prefer ++i over i++" and you won't go wrong. I know that ++i is slightly faster than i++, but thought that there is no…
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
-3
votes
3 answers

C Programming increment & decrement

for(i=0;i++<10;) { printf("%d\n",i); } Why is it printing 1 to 10? I know post increment happens after a loop, so why is it not showing 0? And why is it showing 10?
azmain
  • 19
  • 4
-3
votes
2 answers

Mathematical representation of ++a

We can define a++ as a = a + 1; Which is self-explaining that it makes a copy of a then adds 1 to it and puts the result in a. But can we define ++a the same way? Maybe the answer is very simple but I'm unaware of it.
SSH
  • 1,609
  • 2
  • 22
  • 42
-3
votes
1 answer

Apparently ++ means +4 or +8 for an Objective-C integer property

For some reason, when I try to increment one of my integers, it increments by four instead of one! The only thing I can think of is something wrong in the .h file. The integer is declared like this: @property (nonatomic, assign) int *number; and in…
Kiley
  • 409
  • 1
  • 5
  • 19
-3
votes
1 answer

How does the C++ postincrement in g++ work

I know how does the postincrement in C++ work. Also, the C++ standard says that the behaviour of things like y = x++ + x is undefined, as there is no guarantee for the order in which are the operands of + evaluated. However, I've heard that g++…
Cris
  • 162
  • 1
  • 8
-3
votes
2 answers

Post Increments

I'm trying to understand post incrementation at the hand of these 3 examples. But I have difficulties trying to understand the last one. 1. int x = 0; x++; System.out.println(x); //prints out 1 2. int x = 0; x = x++; System.out.println(x);…
Helenesh
  • 3,999
  • 2
  • 21
  • 36
-3
votes
1 answer

Post operator evaluation order

My friend and I came upon an interesting joke C > C++ since he likes C over C++. However, I am curious as to the validity of executing that code segment. I ran and compiled int C; C = 1;std::cout << (C++ < C) << "\n"; C = 1;std::cout << (C < C++) <<…
kikiotsuka
  • 79
  • 8
-3
votes
1 answer

How the pre and post increment work in c++?

i want to know if increment/decrement operator results vary from compiler to compiler or it is independent of the compiler. as I am getting different results in different compilers of c/c++ (tested in G++, TDM, gcc) for the same expression.
Vimlesh
  • 109
  • 1
  • 5
-3
votes
2 answers

Increment Operator Magic in C#

Why is the Output of this code gives the value 100. Please help me to understand this behavior. static void Main(string[] args) { int i = 100; for (int n = 0; n < 100; n++) { i = i++; } …
Akshay Joy
  • 1,765
  • 1
  • 14
  • 23