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
4 answers

what is the difference between printf and cout

Some code like this below: int x = 1; printf("%d,%d,%d",x,x++,x); //A statement cout<
guyuequan
  • 75
  • 1
  • 11
3
votes
4 answers

Bash Post Increment

Just a little question about the right way of doing Post-increment in bash. while true; do VAR=$((CONT++)) echo "CONT: $CONT" sleep 1 done VAR starts at 1 in this case. CONT: 1 CONT: 2 CONT: 3 But if I do this: while true; do echo "CONT:…
JorgeeFG
  • 5,651
  • 12
  • 59
  • 92
3
votes
1 answer

Increment integers by one and ternary operator

I am new to Python, how can I do the following C idiom in Python? i += 1 i++ i = (j == 2)? 1 : 0 Thank you.
michael
  • 106,540
  • 116
  • 246
  • 346
3
votes
2 answers

unary operator overloading special case in c++

I have success fully overloaded unary ++,-- postfix/prefix operator and my code works fine, but when ever in use (++obj)++ statement it returns unexpected result here is code class ABC { public: ABC(int k) { i = k; } ABC…
Sourabh
  • 190
  • 8
3
votes
4 answers

post increment behaviour

i have small doubt.why the below code is printing value i=2. int i=2; i=i++; System.out.println(i); can someone please explain me what is happening in line no 2. so there is no meaning here of doing ++ here? Thanks
Jayesh
  • 6,047
  • 13
  • 49
  • 81
2
votes
2 answers

Why do textbooks prefer "++x" to "x++" when it is context invariant?

Possible Duplicate: Difference between i++ and ++i in a loop? Is there a performance difference between i++ and ++i in C++? Incrementing in C++ - When to use x++ or ++x? Why use ++i instead of i++ in cases where the value is not used anywhere…
user1202733
  • 623
  • 4
  • 10
2
votes
3 answers

Pre / Post Increment Explanation

Please be easy on me and don't shoot me as I'm still newbie. I'm totally confused and can't for life figure out why when I run this code: int y = 9; cout << "++y = " << ++y << "\n--y = " << --y << "\ny++ = " << y++ << "\ny-- = " << y-- << "\n"; cout…
XO39
  • 481
  • 2
  • 9
  • 22
2
votes
5 answers

Are a[i]=y++; and a[i++]=y; undefined behavior or unspecified in C language?

When I was looking for the expression v[i++]=i; why it is to define the behavior, I suddenly saw an explanation because the expression exists between two sequence points in the program, and the c standard stipulates that in the two sequence points …
2
votes
2 answers

double free or corruption (out) in realloc

I'm trying to fix a problem in my code since several days but I'm still stuck on it. I want to insert a value in a tab through a realloc but I have a memory leak (or something else) and I don't know why. Here's my code: #include #include…
Ouahib
  • 33
  • 5
2
votes
1 answer

Confusion with increment operators in Java in conditional statements

int i = 10; if(i++ == i) System.out.println(i + " is good"); else System.out.println(i + " is bad"); int j = 20; if(++j == j) System.out.println(j + " is good"); else System.out.println(j + " is bad"); So when thinking about the output my thought…
2
votes
4 answers

Unexpected result when looping chars in C++

I'm using the book "Programming Principles and Practice using C++" to learn programming and one of the exercises is looping through the characters a-z using a while-loop. Now, I have programmed with C++ and other languages before, so I decided to…
thomasvakili
  • 1,138
  • 2
  • 10
  • 17
2
votes
2 answers

Java: Order of Operations, Post-Increment Clarification

Why is the output 25? // CODE 1 public class YourClassNameHere { public static void main(String[] args) { int x = 8; System.out.print(x + x++ + x); } } Hi! I am aware that the above code will print 25. However, I would like to…
2
votes
2 answers

increments order in java

Why in java this code outputs 1: int bracketsTest = 0; int resultBrackets = 1 + bracketsTest + bracketsTest++; // return 1 But this outputs 2: int bracketsTest = 0; int resultBrackets = 1 + bracketsTest++ + bracketsTest; // return 2 I lived in…
2
votes
8 answers

Operator associativity, precedence

I just wonder if, for the following code, the compiler uses associativity/precedence alone or some other logic to evaluate. int i = 0, k = 0; i = k++; If we evaluate based on associativity and precedence, postfix ++ has higher precedence than =,…
2
votes
2 answers

Why does == have higher precedence than postfix ++ in Java?

Please, could someone help me to figure out why equality has higher priority before postfix here? int x = 6; System.out.println(7 == x++); Output: false According to some sources of precedence of operators in Java: postfix should have higher…